gpt4 book ai didi

linux - sed 下一个 & 下一个命令

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:36 25 4
gpt4 key购买 nike

想知道是否有人可以解释 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) 吗?以及在哪里使用下一个 (n) 命令。

如果我使用 sed 's/Owner and Operator Guide/Installation Guide/' input.txt 将按预期更改所有内容。

最佳答案

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

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

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

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

关于linux - sed 下一个 & 下一个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25946273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com