2013-03-20 109 views
0

我有這樣如何編寫包含awk的逗號

field1****This is, sample text ", here and there"@@@@field2****This is, sample text ", here and there"@@@@field3****This is, sample text ", here and there"@@@@ 

**** is the field separator

@@@@ is the record separator

文件我想用CSV CSV文件中像這樣

field1, "This is, sample text ", here and there"" 

所以我可以在Microsoft Excel中打開它,並在兩欄顯示

回答

1
$ cat file 
field1****This is, sample text ", here and there"@@@@field2****This is, sample text ", here and there"@@@@field3****This is, sample text ", here and there"@@@@ 

$ gawk -F'\\*\\*\\*\\*' -v OFS=', ' -v RS='@@@@\n?' '{$2="\"" $2 "\""}1' file 
field1, "This is, sample text ", here and there"" 
field2, "This is, sample text ", here and there"" 
field3, "This is, sample text ", here and there"" 

$ gawk -F'\\*\\*\\*\\*' -v OFS=', ' -v RS='@@@@\n?' '{for (i=1;i<=NF;i++) $i="\"" $i "\""}1' file 
"field1", "This is, sample text ", here and there"" 
"field2", "This is, sample text ", here and there"" 
"field3", "This is, sample text ", here and there"" 
+0

我有大的文本文件。如果我在Excel中打開新文件,那麼來自column2的一些行將作爲下一個記錄的column1。 – user1865341 2013-03-20 04:43:09

+0

我並不感到驚訝,因爲你所要求的輸出格式看起來很奇怪。用導致問題的輸入和輸出更新你的問題。 – 2013-03-20 19:16:07