我有以下CSV文件:
"test","test2","test
3 and some other
data"
"test4","test5","test6 and some other
data"
我想用字符\n
替换所有换行符(windows或unix样式),所以我会得到:
"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
}'
答案 0 :(得分:2)
这是一种方法:
$ 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"
答案 1 :(得分:1)
这对你有用吗?两条有相同想法的线
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"
答案 2 :(得分:1)
这可能适合你(GNU sed):
sed -r ':a;/^("[^"]*",?){3}$/!{$!N;s/\n/\\n/;ta};P;D' file
或在紧要关头:
sed ':a;/"$/!{$!N;s/\n/\\n/;ta};P;D' file