How to ignore blank lines and comment lines with awk

I am writing this code:

awk -F'=' '!/^$/{arr[$1]=$2}END{for (x in arr) {print x"="arr[x]}}' 1.txt 2.txt 

this code ignores empty lines, but I also want to ignore a line starting with C # (comments).

Any idea how to add multiple templates?

+4
source share
3 answers

Change !/^$/ To

 !/^($|#)/ 

or

 !/^($|[:space:]*#)/ 

if you want to ignore spaces before # .

+11
source
 awk 'NF && $1!~/^#/' data.txt 

Print all non-empty lines (the number of NF fields will not be zero) and lines that do not contain # as the first field.

It will handle the string of spaces correctly, since NF will be zero, and leading spaces, since $1 will ignore them.

+8
source
 awk 'NF && !/^[:space:]*#/' data.txt 

Because '[: space:] *' has no spaces.

0
source

All Articles