繁体   English   中英

sed next和Next命令

[英]sed next & Next command

想知道是否有人可以解释sed中的Next(N)和next(n)命令之间的区别。 我正在尝试跟随。

这是我的输入文件

Consult Section 3.1 in the Owner and Operator Guide
Consult Section 3.1 in the Owner and Operator Guide
for a description of the tape drives
available on your system.

我正在使用以下命令(下一条命令)

/Operator/{
n
s/Owner and Operator Guide/Installation Guide/
}

结果是

Consult Section 3.1 in the Owner and Operator Guide
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
available on your system.

然后我正在使用以下

 /Operator/{
 N
 s/Owner and Operator Guide/Installation Guide/
 }

结果是

Consult Section 3.1 in the Installation Guide
Consult Section 3.1 in the Owner and Operator Guide
for a description of the tape drives
available on your system.

您能解释一下为什么我得到两个不同的结果吗? 我仅需要在多行图案空间中使用Next命令(N)吗? 以及在哪里使用next(n)命令。

如果我使用sed的“ s /所有者和操作员指南/安装指南/”,input.txt将按预期更改所有内容。

n命令打印出模式空间(除非使用了“ -n”标志),清除模式空间,然后读入下一行。 在您的示例中

/Operator/{
n
s/Owner and Operator Guide/Installation Guide/
}

/Operator/与第一行匹配。 执行n命令后,我们将打印模式空间,以便您的第一行照原样打印。 第二行被加载到模式空间并执行替换。 结果,您在第二行看到更改。

n命令不同, N命令不会打印当前模式空间,也不会清除它。 而是将下一行追加到由换行符分隔的模式空间。 结果,由于您没有使用全局标志,因此替换操作在第一行进行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM