我想加入以|
结尾的行与下一行。
示例:
abcd|
ef
123456|
78|
90
期望的输出:
abcdef
1234567890
答案 0 :(得分:2)
这可能适合你(GNU sed):
sed ':a;N;s/|\s*\n//;ta;P;D' file
答案 1 :(得分:1)
使用GNU sed:
$ sed ':a;/| *$/{N;ba};s/| *\n//g' infile
abcdef
1234567890
执行以下操作:
:a # Label to branch to
/| *$/{ # If the line ends with a pipe and optional spaces
N # Append next line to pattern space
ba # Branch to label
}
s/| *\n//g # Remove all pipes followed by optional spaces and a newline
要在BSD sed下工作,必须在标签和分支命令之后拆分命令:
sed -e ':a' -e '/| *$/{N;ba' -e '};s/| *\n//g' infile
答案 2 :(得分:1)
使用GNU awk进行多字符RS:
$ awk -v RS='[|]\\s*\\n' -v ORS= '1' file
abcdef
1234567890
对于其他问题,一种方法是:
$ awk 'sub(/\|[[:blank:]]*$/,"") {s = s $0; next} {print s $0; s=""}' file
abcdef
1234567890
如果您的上一个输入行可以以|
结尾,则需要告诉我们如何处理该问题并将示例输入/输出添加到您的问题中。
答案 3 :(得分:0)
您可以尝试此sed
,
sed ':loop; /| *$/{N;s/\n//g}; t loop' yourfile | sed 's/ *| *//g'
答案 4 :(得分:0)
awk
救援!以下脚本在发生时删除管道字符和换行符。
$ awk '{c=sub(/\|/,"")?"":"\n"; $1=$1; printf "%s%s",$0,c}' file
abcdef
1234567890
答案 5 :(得分:0)
我从这个
开始awk -F '|' 'NF>1 {printf "%s", $1} NF==1' file
但后来我认为对你可能拥有多少个字段做了太多假设。所以我最终得到了
awk -F '|' '{if (NF>1 && $NF ~ /^[[:blank:]]*$/) {NF--; printf "%s", $0} else print}' file
更坚固但更简洁。
答案 6 :(得分:0)
考虑输入文本存储在文件名infile
您可以使用此GNU sed命令:
sed ':begin;$!N;s/\n//;s/\s//g;s/|//g' infile
答案 7 :(得分:0)
awk '{sub(/\|/,"")}{printf "%s",$1}/f/{print "\r"}END {print ""}' file
abcdef
1234567890