How to show a script without comments?
Joel Roth
joelz at pobox.com
Mon Aug 1 19:25:48 UTC 2016
Ralf Mardorf wrote:
> Hi,
>
> how can I display a shell script without comments and empty lines, but
> also without cutting commands?
>
> This is an example text file:
>
> [weremouse at moonstudio tmp]$ cat test.txt
> ## test file.
> test # test
> test 2
> echo "Hello #"
> echo "Hello #" # Oops
>
> A simple egrep line, doesn't remove all comments:
>
> [weremouse at moonstudio tmp]$ egrep -v "^#|^$" test.txt
> test # test
> test 2
> echo "Hello #"
> echo "Hello #" # Oops
>
> A simple sed line, does remove all comments, but cuts commands:
>
> [weremouse at moonstudio tmp]$ sed "s/#.*$//g" test.txt | grep -v ^$
> test
> test 2
> echo "Hello
> echo "Hello
Hi Ralf,
This works for double quotes. Removes blank lines.
cat test.txt | perl -nlE 's/#[^"]*$//g; say unless /^\s*$/'
This won't work because of shell quoting rules.
cat test.txt | perl -nlE 's/#[^'"]*$//g; say'
In this case, you'll need a stand-alone script, I think.
cheers,
--
Joel Roth
More information about the ubuntu-users
mailing list