2014-09-20 179 views
0

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

这是以下我的输入文件

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的/ Owner和Operator Guide/Installation Guide /'input.txt将按预期改变所有内容。

回答

2

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

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

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

n命令不同,N命令不会打印当前模式空间,也不会清除它。相反,它会将下一行附加到由换行符分隔的模式空间。因为你没有使用全局标志,所以你的替换是在第一行执行的。