Home > Mobile >  Negate condition with awk
Negate condition with awk

Time:03-26

I cant seem to figure out how to negate my condition with awk.

For example, lets say I have

g ha
a bb
g hc
f cd
t de
g pf

I can print just the ones with g and starts with h for the first and second column with:

cat test.txt | awk '$1 ~ /g/ && $2 ~ /^h/'

but how do I do the opposite, where I just want to print the ones that are not g and start with h. Basically to have the output:

a bb
f cd
t de
g pf

CodePudding user response:

If $1 ~ /g/ && $2 ~ /h/ is true then continue with the next line otherwise output the current line.

awk '$1 ~ /g/ && $2 ~ /h/{next} {print}' test.txt

Output:

a b
f c
t d
g p
  • Related