简体   繁体   中英

How to print all lines from a CSV file that contains the exact match of a string from the nth column?

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" 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"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM