sed 将行连接在一起
将文件中不以字符“0”结尾的行连接在一起的 sed (或其他工具)命令是什么?
我会有这样的台词
412|n|领先建筑材料
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
d|d|20||0
需要单独保留,然后我会有像这样的行(有 3 行,但只有一行以 0 结尾)
需要连接/组合成一行
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)
which need to be joined/combined into one line
在循环中(分支
ba
到标签:a
),如果当前行以 0 结尾 (/0$/
) 则追加下一行 (N
)并删除内部换行符(s/\n//
)。awk:
Perl:
bash:
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:
Perl:
bash:
awk 也可能很短:
测试:
awk could be short too:
test:
这个答案的评级令人惊讶
;s
(这个关于 sed 替换的惊讶眨眼表情双关语是故意的)给定OP规范:sed将行连接在一起。此提交的最后一条评论
还建议检查相同的答案。
IE。使用
sed ':a;/0$/{N;s/\n//;ba}'
逐字检查,这不会给出(进行测试;):
要获得此用途:
或:
not:
注意:
不使用分支和
等同于:
H
开始所有序列d
短路当前行上的进一步脚本命令执行并开始下一个周期,因此地址选择器遵循/0$/!
只能是/0$/!!
所以的地址选择器
/0$/{H;z;x;s/\n//g;}
是多余的,不需要。/0$/!{H;d;}
/0$/{H;z;x;s/\n//g;}
${H;z;x;s/\n//g;p;}
使用/0$/ ...
命令,并带有额外的p
来强制最终打印,并带有现在不必要的z
(清空并重置模式空间,如 s/.*//)The rating of this answer is surprising
;s
(this surprised wink emoticon pun on sed substitution is intentional) given the OP specifications: sed join lines together.This submission's last comment
also suggests checking the same answer.
ie. Check using
sed ':a;/0$/{N;s/\n//;ba}'
verbatimwhich will not give (do the test ;):
To get this use:
or:
not:
Notes:
does not use branching and
is identical to:
H
commences all sequencesd
short circuits further script command execution on the current line and starts the next cycle so address selectors following/0$/!
can only be/0$/!!
so the address selector of/0$/{H;z;x;s/\n//g;}
is redundant and not needed./0$/!{H;d;}
/0$/{H;z;x;s/\n//g;}
${H;z;x;s/\n//g;p;}
uses/0$/ ...
commands with an extrap
to coerce the final print and with a now unnecessaryz
(to empty and reset pattern space like s/.*//)典型的神秘 Perl 单行:
它使用序列“0\n”作为记录分隔符(根据你的问题,我假设每一行都应该以零结尾)。任何记录都不应该有内部换行符,因此它们被删除,然后打印该行,附加删除的 0 和换行符。
对你的问题的另一个看法是确保每行有 17 个管道分隔的字段。这并不假设第 17 个字段值必须为零。
A typically cryptic Perl one-liner:
This uses the sequence "0\n" as the record separator (by your question, I'm assuming that every line should end with a zero). Any record then should not have internal newlines, so those are removed, then print the line, appending the 0 and newline that were removed.
Another take to your question would be to ensure each line has 17 pipe-separated fields. This does not assume that the 17th field value must be zero.
如果以 0 存储结尾,则删除换行符..
if ends with 0 store, remove newline..