2

次の CSV ファイルがあります。

"test","test2","test
3 and some other

data"
"test4","test5","test6 and some other


data"

すべての改行 (Windows または UNIX スタイル) を文字に置き換え\nたいので、次のようになります。

"test","test2","test\n3 and some other\n\ndata"
"test4","test5","test6 and some other\n\n\ndata"

私は試してみましawkたが、成功しませんでした:

cat myFile.csv | awk -F \" -v OFS=\" '{
   for (i=0; i<NF; i++) {
      gsub("\\n", "\\\\n", $i)
   }
   print
}'
4

3 に答える 3

2

1 つの方法を次に示します。

$ awk '!/"$/{sub(/$/,"\\n");printf "%s",$0;next}1' file
"test","test2","test\n3 and some other\n\ndata"
"test4","test5","test6 and some other\n\n\ndata"
于 2013-04-16T12:21:05.070 に答える
1

これはあなたのために働きますか?同じ考えの2行

awk -v RS="\0"  '{gsub(/\n/,"\\n");sub(/\\n$/,"");gsub(/"\\n"/,"\"\n\"");}1' file      

また

awk -v RS="\0" -v ORS="" '{gsub(/\n/,"\\n");sub(/\\n$/,"\n");gsub(/"\\n"/,"\"\n\"")}1' file

あなたのデータで:

kent$  cat file
"test","test2","test
3 and some other

data"
"test4","test5","test6 and some other


data"

出力:

kent$  awk -v RS="\0" -v ORS="" '{gsub(/\n/,"\\n");sub(/\\n$/,"\n");gsub(/"\\n"/,"\"\n\"")}1' file
"test","test2","test\n3 and some other\n\ndata"
"test4","test5","test6 and some other\n\n\ndata"
于 2013-04-16T12:16:56.887 に答える