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?
We can remove blank lines using awk: $ awk NF < myfile.
The IGNORECASE is a built in variable which can be used in awk command to make it either case sensitive or case insensitive. If the IGNORECASE value is 0, then the awk does a case sensitive match. If the value is 1, then the awk does a case insensitive match.
NF is a predefined variable whose value is the number of fields in the current record. awk automatically updates the value of NF each time it reads a record. No matter how many fields there are, the last field in a record can be represented by $NF . So, $NF is the same as $7 , which is ' example.
1 means to print every line. The awk statement is same as writing: awk -F"=" '{OFS="=";gsub(",",";",$2);print $0;}' Copy link CC BY-SA 3.0.
Change !/^$/
to
!/^($|#)/
or
!/^($|[:space:]*#)/
if you want to disregard whitespace before the #
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With