Reputation: 3611
I've the following CSV file:
"test","test2","test
3 and some other
data"
"test4","test5","test6 and some other
data"
I would like to replace all the newlines (windows or unix style) by the character \n
, so I would obtain:
"test","test2","test\n3 and some other\n\ndata"
"test4","test5","test6 and some other\n\n\ndata"
I've tried with awk
but without success:
cat myFile.csv | awk -F \" -v OFS=\" '{
for (i=0; i<NF; i++) {
gsub("\\n", "\\\\n", $i)
}
print
}'
Upvotes: 2
Views: 629
Reputation: 58391
This might work for you (GNU sed):
sed -r ':a;/^("[^"]*",?){3}$/!{$!N;s/\n/\\n/;ta};P;D' file
or at a pinch:
sed ':a;/"$/!{$!N;s/\n/\\n/;ta};P;D' file
Upvotes: 1
Reputation: 195049
does this work for you? two lines with same idea
awk -v RS="\0" '{gsub(/\n/,"\\n");sub(/\\n$/,"");gsub(/"\\n"/,"\"\n\"");}1' file
or
awk -v RS="\0" -v ORS="" '{gsub(/\n/,"\\n");sub(/\\n$/,"\n");gsub(/"\\n"/,"\"\n\"")}1' file
with your data:
kent$ cat file
"test","test2","test
3 and some other
data"
"test4","test5","test6 and some other
data"
output:
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"
Upvotes: 1
Reputation: 85785
Here is one method:
$ 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"
Upvotes: 2