我有以下CSV,用“|”分隔。
101|abc|this is desc|2017 102|xyz|"thie is a long desc des for xyz continue here."|2017
我有两个logging, 101
和102
。 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