[英]sed next & Next command
Wonder if someone can explain the difference between Next (N) and the next(n) command in sed. 想知道是否有人可以解释sed中的Next(N)和next(n)命令之间的区别。 i was trying following. 我正在尝试跟随。
this is my input file 这是我的输入文件
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.
i was using following (next command) 我正在使用以下命令(下一条命令)
/Operator/{
n
s/Owner and Operator Guide/Installation Guide/
}
result is 结果是
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.
then i was using following 然后我正在使用以下
/Operator/{
N
s/Owner and Operator Guide/Installation Guide/
}
result is 结果是
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.
can you please explain why I'm getting two different results ? 您能解释一下为什么我得到两个不同的结果吗? do i need to use Next command (N) only in multiline pattern space ? 我仅需要在多行图案空间中使用Next命令(N)吗? and where to use next (n) command. 以及在哪里使用next(n)命令。
If i use sed 's/Owner and Operator Guide/Installation Guide/' input.txt will change everything as expected. 如果我使用sed的“ s /所有者和操作员指南/安装指南/”,input.txt将按预期更改所有内容。
The n
command prints out the pattern space (unless the "-n" flag is used), clears the pattern space, and reads in the next line. n
命令打印出模式空间(除非使用了“ -n”标志),清除模式空间,然后读入下一行。 In your example, 在您的示例中
/Operator/{
n
s/Owner and Operator Guide/Installation Guide/
}
 /Operator/
is matched for the first line.  /Operator/
与第一行匹配。 After performing the n
command, we print the pattern space so your first line is print as is. 执行n
命令后,我们将打印模式空间,以便您的第一行照原样打印。 Second line is loaded in to pattern space and the substitution is performed. 第二行被加载到模式空间并执行替换。 As a result, you see the changes on the second line. 结果,您在第二行看到更改。
The N
command, unlike n
command, does not print the current pattern space, and does not clear it. 与n
命令不同, N
命令不会打印当前模式空间,也不会清除它。 Instead it appends the next line to the pattern space separated by a newline. 而是将下一行追加到由换行符分隔的模式空间。 As a result your substitution is performed on the first line since you are not using a global flag. 结果,由于您没有使用全局标志,因此替换操作在第一行进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.