Deleting a line which has a certein condition by using awk

I am trying to delete specific line in a gro file.
I want to delete the lines that satisfy the if conditions.The awk code that I am currenly using is this;

cat doubled_system.gro | awk '{if ($2 ~ /^NA/ && $5 > 23)  print $0}' > new.gro

So far i manage the line that I don’t want in ‘new.gro’ file.

I was wondering how can combine sed or grep with this if condition so that I can delete the line that I dont want in doubled_system.gro file?

>Solution :

manage the line that i dont want(…)delete the line that i dont want

If you have negative of desired output you need simply to negate condition, as you have used logical AND then it is valid target for de Morgan rule, therefore after negating

$2 ~ /^NA/ && $5 > 23

we do get

$2 !~ /^NA/ || $5 <= 23

your code does also needlessly use if and cat, as GNU AWK can read file by itself, after fixing that your code might look as follows

awk '($2 !~ /^NA/ || $5 <= 23){print $0}' doubled_system.gro > new.gro

which will save output without undesired line(s) into new.gro, after you check that it is giving correct result for all allowed input values, then you might use -i inplace to place file in place like so

awk -i inplace '($2 !~ /^NA/ || $5 <= 23){print $0}' doubled_system.gro

Leave a Reply