我有以下CSV,由" |"。
隔开101|abc|this is desc|2017
102|xyz|"thie is a long desc
des for xyz continue here."|2017
我有两个记录101
和102
。102记录分为2行。如何使用"/"使用sed
或其他命令行工具来逃避新的行字符?
awk
是处理此操作的更好工具。
假设您知道每行需要多少列。您可以使用此awk
命令:
awk -v n=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 (/[a-z]+$/) { $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