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

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

问题描述

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

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

我将有这样的行

412 | n |领导建材||||||||||| d | d | 20 || 0

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

需要单独放置,然后我将有这样的行(例如,这是3行,但只有一个末端为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 |打结工具|||||打结工具

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

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

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

需要合并/合并为一行的

which need to be joined/combined into one line

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

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

推荐答案

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

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

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/\n//).

确认:

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

Perl:

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

重击:

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

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

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