Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Negate condition with awk

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading