Saya memiliki file .CSV dengan format di bawah ini:
"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",""
Kolom ke-5 file memiliki string yang berbeda. Saya perlu memfilter file berdasarkan nilai kolom ke-5. Katakanlah, saya perlu file baru dari file saat ini yang hanya memiliki catatan dengan nilai "string 1" di bidang kelima.
Untuk ini saya mencoba perintah di bawah ini,
awk -F"," ' { if toupper($5) == "STRING 1") PRINT }' file1.csv > file2.csv
tapi itu membuat saya kesalahan sebagai berikut:
awk: { if toupper($5) == "STRING 1") PRINT }
awk: ^ syntax error
awk: { if toupper($5) == "STRING 1") PRINT }
awk: ^ syntax error
Saya kemudian menggunakan yang berikut ini yang memberi saya hasil yang aneh.
awk -F"," '$5="string 1" {print}' file1.csv > file2.csv
Keluaran:
"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: Saya menggunakan perintah toupper berada di sisi yang aman, karena saya tidak yakin apakah string akan berada dalam huruf kecil atau besar. Saya perlu tahu apa yang salah dengan kode saya dan apakah ruang dalam string penting saat mencari pola menggunakan AWK.
'","'
pembatas, jika tidak maka akan menyelesaikan masalah saya ... solusi hebat ...