我有一個帶有兩列的 csv 資料集,看起來像這樣
Andrew, hello I'm from Germany
Andy, I'm from Cambodia
Arthur, I come from Hong Kong
Alec, I'm of african descent
Richard,I'm chinese
Tess, I'm Tess, i also come from Cambodia
如果我想在此 csv 的第二列中搜索關鍵字“柬埔寨”并回傳如下所示的內容
Andy, I'm from Cambodia
Tess, I'm Tess, i also come from Cambodia
我試過 $ cut -d ',' -f1,2 file | grep -i "\bcambodia\b" 但是這段代碼也搜索第一列,我不想要,我只想搜索第二列,我想在輸出中保留第一列
uj5u.com熱心網友回復:
您可以考慮awk使用逗號作為分隔符
$ awk -F, '/Cambodia/ && $1 !~ /Cambodia/' input_file
Andy, I'm from Cambodia
Tess, I'm Tess, i also come from Cambodia
通過使用,分隔符,您可以排除與字串匹配的第 1$1 !~ /Cambodia/列,同時匹配將包含該字串的所有其他列
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317370.html
