我有一个.CSV文件,格式如下:
"column 1","column 2","column 3","column 4","column 5","column 6","column 7","column 8","column 9","column 10
"12310","42324564756","a simple string with a , comma","string with or, without commas","string 1","USD","12","70%","08/01/2013",""
"23455","12312255564","string, with, multiple, commas","string with or, without commas","string 2","USD","433","70%","07/15/2013",""
"23525","74535243123","string , with commas, and - hypens and: semicolans","string with or, without commas","string 1","CAND","744","70%","05/06/2013",""
"46476","15467534544","lengthy string, with commas, multiple: colans","string with or, without commas","string 2","CAND","388","70%","09/21/2013",""
文件的第5列具有不同的字符串。我需要根据第5列的值过滤掉文件。可以说,我需要一个当前文件中的新文件,该文件的第五个字段中的记录仅包含值“字符串1”。
为此,我尝试了以下命令,
awk -F"," ' { if toupper($5) == "STRING 1") PRINT }' file1.csv > file2.csv
但是它给我抛出如下错误:
awk: { if toupper($5) == "STRING 1") PRINT }
awk: ^ syntax error
awk: { if toupper($5) == "STRING 1") PRINT }
awk: ^ syntax error
然后,我使用以下代码给了我一个奇怪的输出。
awk -F"," '$5="string 1" {print}' file1.csv > file2.csv
输出:
"column 1" "column 2" "column 3" "column 4" string 1 "column 6" "column 7" "column 8" "column 9" "column 10
"12310" "42324564756" "a simple string with a comma" string 1 without commas" "string 1" "USD" "12" "70%" "08/01/2013" ""
"23455" "12312255564" "string with string 1 commas" "string with or without commas" "string 2" "USD" "433" "70%" "07/15/2013" ""
"23525" "74535243123" "string with commas string 1 "string with or without commas" "string 1" "CAND" "744" "70%" "05/06/2013" ""
"46476" "15467534544" "lengthy string with commas string 1 "string with or without commas" "string 2" "CAND" "388" "70%" "09/21/2013" ""
PS:我使用toupper命令是安全的,因为我不确定字符串是小写还是大写。我需要知道我的代码有什么问题,以及在使用AWK搜索模式时字符串中的空格是否重要。
'","'
定界符,否则它将解决我的问题...很好的解决方案...