awk :quick question removing empty line.

How to write in awk to remove lines starting with "#" and then process the file:

This is not working:

cat file|awk '{if ($0 ~ /^#/) $0="";print NF>0}' 

When I just give

cat file|awk '{if ($0 ~ /^#/) $0="";print }'

it prints the blank lines . I don't wnat the blank lines along with the data lines.

Thanks,

Why don't you simply do:

awk '$0 !~ /^#/ && NF' file

If you want to perform any action:

awk '$0 !~ /^#/ && NF{ action here }' file
1 Like

Try:

awk '!/^#/' file
grep -v '^#' file
1 Like

To suppress printing of blank lines and remove lines starting with #

awk '!/^#|^$/' file

PS: no need to cat data to awk

1 Like

Thanks Yoda,
I was looking for something like this

awk '$0 !~ /^#/ && NF{ action here }' file

, it worked great, but it is also removing exiting empty lines, but that is fine in this case,

  • && NF : worked and seems it removed empty lines.

Scrutinizer ,
I dont wanted to use grep -v, as wanted to pass in one awk statement. Thanks again...

Jotne, This worked great as well,

awk '!/^#|^$/' file

,
But surprised it leaves only one blank line on the data file, after it removed all starting hash and blank lines.
Thanks all. .

You may have blanks or tabs in the remaining line.

Try this:

awk 'NF && !/#/ {action}' file
1 Like

If you don't want to remove existing empty lines, then I guess what Scrutinizer posted is the right approach.

Just add your action part to it:

awk '!/^#/ { action here } ' file
1 Like
grep '^[^#]' file
awk '/^[^#]/ {print}' file

removes empty lines, but leaves lines with a blank character.

1 Like

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.

Thanks for the post.

It greps the lines with a first character that is not a #

But a blank line does not start with # , so why does it remove it?

awk '/^[^#]/ {print}' file

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

awk 'NF && !/#/ {action}' file

That would not work. It has two problems.

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:

$ cat -e input
# X$
$
X #$
 $
string 1-99-string 2$
$ awk 'NF && !/#/ {print}' input
string 1-99-string 2
$ awk '/^[^#]/ {print}' input | cat -e
X #$
 $
string 1-99-string 2$
$ grep -v -e '^#' -e '^$' input | cat -e
X #$
 $
string 1-99-string 2$

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.

1 Like

Also hanson44 & Scrutinizer, thanks ..,

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 #.

awk '$1~/^[^#]/' file

Thanks.