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
  • 25,269
  • 4
  • 47
  • 72
Vituvo
  • 1,008
  • 1
  • 9
  • 29
  • What is your question? I'm not quite sure what you try to achieve... Do you need to print all lines with the occurrence of *ac*? – agentsmith Aug 17 '19 at 20:19
  • @agentsmith I actually want to print all lines, in the second column, that only contain ac between the double-quotes. I'll update the post. – Vituvo Aug 17 '19 at 20:20
  • I assume the output given by ```grep``` is the expected result? Because that's exactly what grep is doing. – agentsmith Aug 17 '19 at 20:24
  • @CuriousSam Did you mean: ```cat file.txt | grep "ac"```? – agentsmith Aug 17 '19 at 20:27
  • @agentsmith Yes, I did. My apologies, long day. – Vituvo Aug 17 '19 at 20:34
  • 1
    @oguzismail ```awk -F, '$2=="ac"' file.txt > temp``` gives me an empty file. – Vituvo Aug 17 '19 at 20:36
  • @oguzismail I also tried ```awk -F, '$2 ~ "ac"' file.txt > temp``` but it that also included ```"12","acute","huu","1991","soyo"``` and that has the string ```acute``` in the second column – Vituvo Aug 17 '19 at 20:45
  • See also [useless use of `cat`](/questions/11710552/useless-use-of-cat) – tripleee Aug 18 '19 at 17:14

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
  • 88,541
  • 4
  • 65
  • 105