Sed:用“-z"替换换行符? [英] Sed: replacing newlines with "-z"?

查看:62
本文介绍了Sed:用“-z"替换换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:用sed替换一些正则表达式为\n.

Problem: replace some regex with \n with sed.

解决方案:有很多类似的答案[1][2][3][4],以及我不会链接的许多其他链接.他们都建议你创建一个新标签:a,合并行N,如果不是文件结束,则分支到:a$!ba,然后执行一些命令.

Solution: there are many similar answers [1][2][3][4], and many other links that I won't link. All of them suggest you to create a new label :a, merge lines N, branch to :a if not end-of-file $!ba, and then do some command.

也就是说……在 GNU sed 手册中,有 -z 选项:

That said... In the GNU sed manual, there is the -z option:

-z
--null-data
--zero-terminated

Treat the input as a set of lines, each terminated by a zero byte
(the ASCII ‘NUL’ character) instead of a newline. This option can
be used with commands like ‘sort -z’ and ‘find -print0’ to process
arbitrary file names. 

所以,首先,出于比较的原因,如果我们尝试幼稚的方法:

So, first, for comparison reasons, if we try the naive approach:

$ seq 3 | sed 's/\n/ /g'
1
2
3

但是,使用这个 -z 选项:

However, using this -z option:

$ seq 3 | sed -z 's/\n/ /g'
1 2 3

真正的问题:为什么?

鉴于它合并"了文档中指定的所有行,我预计我将不得不使用 \0 而不是 \n,因为:

将输入视为一组行,每行以零字节结束(ASCII 'NUL' 字符)

Treat the input as a set of lines, each terminated by a zero byte (the ASCII ‘NUL’ character)

由于我没有找到任何与它相关的帖子,我想我可能在这里误解了一些东西......那么,它到底有什么作用?为什么有效?

Since I didn't find any post related to it, I think I might be misunderstanding something here... So, what does it really do? Why does it work?

推荐答案

使用 -z 改变了 sed 认为是一行的内容.\n 仍然是 \n,但它不是结束一行,而是空字符(在 Sed 中表示为 \x0)将.由于seq 的输出中没有空字节,整个输出被视为一行并在单次迭代中处理(即用空格替换所有\n).

Using -z changes what sed considers to be a line. \n remains \n, but it doesn't end a line, but the null character (which is represented as \x0 in Sed) would. As there are no null bytes in the output of seq, the whole output is considered one line and processed in single iteration (i.e. replacing all \n's by spaces).

这篇关于Sed:用“-z"替换换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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