我有以下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
或其他命令行工具使用" /"来转义新行字符?
答案 0 :(得分:0)
对于您的具体情况,您可以使用此功能。
$ 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
答案 1 :(得分:0)
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
答案 2 :(得分:0)
.mat