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

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?

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

>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
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