MadeInGermany ,
Thanks this is also a nice code, seems inside [ ] the caret is doing negetion for #.
But couldn''t get how the empty lines are getting removed.
awk '/^[^#]/ {print}' file
Is that means print lines starting with something , but not hash and empty lines.
It prints lines that start with a character that is not #. A blank line does not start with a character. So a blank line is not printed, so the blank line is removed (as well as a line starting with #).
-------------------------------
An alternative way, maybe easier to understand, using grep:
grep -v -e '^#' -e '^$' file
This means "print all lines except those that start with # or that are blank".
Ok, now I understand, thanks.
So ii there are lines that do contain spaces or tabs it will not work. awk '/^[^#]/ {print}' file
This would. awk 'NF && !/#/ {action}' file
First, it would need to be !/^#/ to mean "do not act on lines that start with #".
Second, the idea was to act on lines that have a space character. But NF is 0 if just a space character, just as NF is 0 if the line is totally blank. So it would not act on the lines with spaces.
-----------------------
Look at the test input below, where the expected output is to NOT print first two lines (empty, start with #), and do print last three lines, and compare the results from the three ways:
Indeed, with hand-edited files in particular there are often empty lines that are not entirely empty and that contains some form of spacing. Also, lines with only comment can be indented for clarity and therefore do not always start at the first position on the line. So in general I would tend to use:
awk '$1~/^[^#]/' file
To exclude empty lines and lines with only comment that may contain leading spacing and
awk '$1!~/^#/' file
To only exclude lines with only comment and that may contain leading spacing and
awk NF
to only exclude empty lines that that may contain tabs or spaces.
Scrutinizer,
Thanks much , this worked... to remove empty lines, and comments( starting with # ) and seems to be fixed earlier issues, It worked even the file has a space and then it has the comment starting with #.