1

I want to print all lines from a CSV file that match the character string "ac". So if column 2 equals "ac", then print this line.

Before

"id","make","model","yeear","product"
"11","ac","tot","1999","9iuh8b"
"12","acute","huu","1991","soyo"
"13","ac","auu","1992","shjoyo"
"14","bb","ayu","2222","jkqweh"
"15","bb","ac","8238","smiley"

After

"11","ac","tot","1999","9iuh8b"
"13","ac","auu","1992","shjoyo"

I attempted cat file| grep "ac", but this will give me all lines that have ac:

"11","ac","tot","1999","9iuh8b"
"12","acute","huu","1991","soyo"
"13","ac","auu","1992","shjoyo"
"15","bb","ac","8238","smiley"
kvantour
  • 22,845
  • 4
  • 45
  • 58
Curious Sam
  • 893
  • 1
  • 6
  • 26

1 Answers1

3

Consider surrounding double quotes:

$ awk -F, '$2=="\"ac\""' input.csv
"11","ac","tot","1999","9iuh8b"
"13","ac","auu","1992","shjoyo"

Or the same with regex pattern matching:

$ awk -F, '$2~/^"ac"$/' input.csv
"11","ac","tot","1999","9iuh8b"
"13","ac","auu","1992","shjoyo"
RomanPerekhrest
  • 75,918
  • 4
  • 51
  • 91