harsh
harsh

Reputation: 79

Parsing csv using conditional statement in awk

I have a csv file

file.csv

C75ADANXX,5,20,,AGGCAGAA,AGAGTAGA,,,,,AB
C75ADANXX,5,21,,AGGCAGAA,GTAAGGAG,,,,,AB
C75ADANXX,5,22,,AGGCAGAA,ACTGCATA,,,,,AB
C75ADANXX,5,23,,AGGCAGAA,AAGGAGTA,,,,,TC
C75ADANXX,5,24,,AGGCAGAA,CTAAGCCT,,,,,TC
C75ADANXX,5,25,,TCCTGAGC,GCGTAAGA,,,,,TC

when I run the following awk command :

awk -F "," '{print$11}' file.csv  ##prints last cloumn

i want to extract lines with TC ; but the following command prints nothing

awk -F "," '{if($11==TC){print$0}}' file.csv

Where am I going wrong in writing the command ? Thank you.

Upvotes: 1

Views: 1032

Answers (3)

malmo
malmo

Reputation: 524

try this one

grep ",TC$" file.csv

Upvotes: 0

harsh
harsh

Reputation: 79

modified the command to

awk -F "," '{if($11=="TC\r"){print$0}}' file.csv 

this file was copied from windows , it had a carriage return character at the end of the line which was obviously not seen when you print only last column.

Upvotes: 1

Akshay Hegde
Akshay Hegde

Reputation: 16997

Modify

awk -F "," '{if($11==TC){print$0}}' file.csv

To

awk -F "," '{if($11=="TC"){print$0}}' file.csv

OR even simple

awk -F, '$11=="TC"' file.csv
  • if($11==TC) Since variable TC is not defined before (because no quotes used, awk treats TC as variable not as string), it evaluates false always, so it prints nothing.

Upvotes: 0

Related Questions