Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a csv file with the following headers - Interaction_ID, PDB_ID, First_Residue, First_Chain, Second_Residue, Third_Residue, Third_Chain & Pattern:

AM_AUTO_1,1cx0,A166,B,C118,B,G129,B,A-minor Type I ACG                            
AM_AUTO_2,1cx0,A165,B,C119,B,G128,B,A-minor Type II ACG                           
AM_AUTO_3,1cx0,A166,B,C119,B,G128,B,A-minor Type II ACG                           
AM_AUTO_4,1ddy,A25,A,C21,A,G8,A,A-minor Type I ACG                            
AM_AUTO_5,1e7k,A33,C,C28,C,G45,C,A-minor Type 0 AGC                            
AM_AUTO_6,1e7k,A33,D,C28,D,G45,D,A-minor Type 0 AAU

What I would like to do is delete rows in the csv file where Pattern = %Type 0% and then print the resulting csv file. The result should be like this:

AM_AUTO_1,1cx0,A166,B,C118,B,G129,B,A-minor Type I ACG                            
AM_AUTO_2,1cx0,A165,B,C119,B,G128,B,A-minor Type II ACG                           
AM_AUTO_3,1cx0,A166,B,C119,B,G128,B,A-minor Type II ACG                           
AM_AUTO_4,1ddy,A25,A,C21,A,G8,A,A-minor Type I ACG  

How I can do this using awk?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

With awk, you can do:

$ awk '$(NF-1)!=0' file
AM_AUTO_1,1cx0,A166,B,C118,B,G129,B,A-minor Type I ACG
AM_AUTO_2,1cx0,A165,B,C119,B,G128,B,A-minor Type II ACG
AM_AUTO_3,1cx0,A166,B,C119,B,G128,B,A-minor Type II ACG
AM_AUTO_4,1ddy,A25,A,C21,A,G8,A,A-minor Type I ACG

or using grep:

grep -v 'Type 0' file
share|improve this answer
    
Just what I wanted. Thank you very much! –  Sri 23 hours ago

Using awk:

awk -F, '$9!~/Type 0/{print $0}' file
AM_AUTO_1,1cx0,A166,B,C118,B,G129,B,A-minor Type I ACG
AM_AUTO_2,1cx0,A165,B,C119,B,G128,B,A-minor Type II ACG
AM_AUTO_3,1cx0,A166,B,C119,B,G128,B,A-minor Type II ACG
AM_AUTO_4,1ddy,A25,A,C21,A,G8,A,A-minor Type I ACG

Explanation:

This causes awk to use , as delimiter (-F,). If the 9th field doesn't contain "Type 0" ($9!~/Type 0/), print the line ({print $0}).

And a sed solution:

sed '/,[^,]*Type 0[^,]*$/d' file
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.