使用sed或其他命令行工具转义CSV文件

我有以下CSV,用“|”分隔。

101|abc|this is desc|2017 102|xyz|"thie is a long desc des for xyz continue here."|2017 

我有两个logging, 101102 。 102条logging分为2行。 我如何使用sed或其他命令行工具以“/”来转义新行字符?

awk是处理这个问题的更好的工具。

假设你知道每行需要多少列。 你可以使用这个awk命令:

 awk -vn=4 -F '|' 'p+NF<n{p+=NF-1; print $0 "\\"; next} {p=0} 1' file 101|abc|this is desc|2017 102|xyz|"this is a long desc\ des for xyz continue here."|2017 

对于你的具体情况,你可以使用这个。

$ cat lll 101|abc|this is desc|2017 102|xyz|"thie is a long desc des for xyz continue here."|2017 105|xyz|"thie is a long desc des for xyz continue here."|2017 101|abc|this is desc|2017 101|abc|this is desc|2017 101|abc|this is desc|2017 105|xyz|"thie is a long desc des for xyz continue here."|2017

$ perl -F'\n' -ane 'BEGIN{our $line_to_join = undef; } foreach ( @F) { if (/[az]+$/) { $line_to_join = $_; } elsif ($line_to_join){ print $line_to_join,"/\n",$_,"\n"; $line_to_join = undef; }else{print $_,"\n" ; $line_to_join = undef;}} ;' < lll

输出:

101|abc|this is desc|2017 102|xyz|"thie is a long desc/ des for xyz continue here."|2017 105|xyz|"thie is a long desc/ des for xyz continue here."|2017 101|abc|this is desc|2017 101|abc|this is desc|2017 101|abc|this is desc|2017 105|xyz|"thie is a long desc/ des for xyz continue here."|2017

 $ sed '/102/ s#$#/&#' file11 101|abc|this is desc|2017 102|xyz|"thie is a long desc/ des for xyz continue here."|2017