2013-03-20 66 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