kumar
kumar

Reputation: 389

how to ignore blank lines and comment lines using 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 ignore blank lines, but i also want to ignore line starting with # (comments).

Any idea how to add multiple patterns?

Upvotes: 6

Views: 21342

Answers (3)

Jaap
Jaap

Reputation: 31

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

Because '[:space:]*' catches none or more spaces.

Upvotes: 3

Levon
Levon

Reputation: 143022

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

Will print all non-blank lines (number of fields NF is not zero) and lines that don't contain # as the first field.

It will handle a line of whitespace correctly since NF will be zero, and leading blanks since $1 will ignore them.

Upvotes: 10

chaos
chaos

Reputation: 124297

Change !/^$/ to

!/^($|#)/

or

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

if you want to disregard whitespace before the #.

Upvotes: 13

Related Questions