jxn
jxn

Reputation: 8035

awk print rows of CSV file on condition?

I have a CSV file that has this values:

A     B       C
1    cat      cat
2    mouse    pig
3    dog      dog

How can i write an awk code that only prints rows where col A = col C?

Output i hope to get:

A     B       C
1    cat      cat
3    dog      dog

Upvotes: 1

Views: 1557

Answers (2)

Claes Wikner
Claes Wikner

Reputation: 1517

awk '!/2/ {print $0}' file
A B C
1 cat cat
3 dog dog

Realized I have to look into advanced help to get the output in order, but failed for the tabulator.

Claes

Upvotes: 0

nu11p01n73R
nu11p01n73R

Reputation: 26667

That is pretty simple

$ awk '$2==$3' file
1    cat      cat
3    dog      dog
  • $2==$3 Checks if the second field, $2 is equal to third field, $3. If yes, awk takes the default action to print the entire record, line.

Upvotes: 4

Related Questions