sed 将行连接在一起 [英] sed join lines together

查看:25
本文介绍了sed 将行连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将文件中不以字符0"结尾的行连接在一起的 sed(或其他工具)命令是什么?

我会有这样的台词

<块引用>

412|n|Leader 建筑材料|||||||||d|d|20||0

需要单独留下,然后我会有这样的行,例如(这是 3 行,但只有一个结尾是 0)

<块引用>

107|n|打结工具|||||打结工具|||||d|d|0||0

哪些需要合并/合并成一行

<块引用>

107|n|打结工具|||||打结工具|||||d|d|0||0

解决方案

 sed ':a;/0$/{N;s/
//;ba}'

在循环中(分支ba到标签:a),如果当前行以0结尾(/0$/)追加下一行 (N) 并删除内部换行符 (s/ //).

awk:

awk '{while(/0$/) { getline a;$0=$0 一个;sub(/
/,_) };打印}'

Perl:

perl -pe '$_.=<>,s/
//while/0$/'

bash:

while read line;做if [ ${line: -1:1} != "0" ] ;然后回声 $line否则 echo -n $line菲完毕

what would be the sed (or other tool) command to join lines together in a file that do not end w/ the character '0'?

I'll have lines like this

412|n|Leader Building Material||||||||||d|d|20||0

which need to be left alone, and then I'll have lines like this for example (which is 3 lines, but only one ends w/ 0)

107|n|Knot Tying Tools|||||Knot Tying Tools

|||||d|d|0||0

which need to be joined/combined into one line

107|n|Knot Tying Tools|||||Knot Tying Tools|||||d|d|0||0

解决方案

 sed ':a;/0$/{N;s/
//;ba}'

In a loop (branch ba to label :a), if the current line ends in 0 (/0$/) append next line (N) and remove inner newline (s/ //).

awk:

awk '{while(/0$/) { getline a; $0=$0 a; sub(/
/,_) }; print}'

Perl:

perl -pe '$_.=<>,s/
// while /0$/'

bash:

while read line; do 
    if [ ${line: -1:1} != "0" ] ; then 
        echo $line
    else echo -n $line
fi
done 

这篇关于sed 将行连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆