9. 打印标志(p 标志)
最后更新时间 (CST):2023-07-20 05:50:12 +0800
sed 替换标志 p
代表 print
。 当替换成功时,它会打印更改的行。
与 sed 中的大多数打印命令一样,它在与-n
选项结合使用时最有用,可以抑制所有行的默认打印。
替换示例
以下是几个简单的替换示例。
Example1:仅打印被替换命令更改的行
$ sed -n 's/John/Johnny/p' employee.txt
101,Johnny Doe,CEO
Example2:将出现 "locate"
的第二个实例更改为 "find"
并打印结果
$ sed -n 's/locate/find/2p' substitute-locate.txt
locate command is used to find files
locate command uses database to find files
注意:在我们的数字标志示例中,我们使用/2
将第二次出现的 "locate"
更改为 "find"
。
在locate.txt
的第 3 行上,没有出现第二次,并且该行上从未发生过替换。
所以将 p
标志添加到我们之前使用的命令中将只打印发生了更改的两行。
如未另行说明,那么本页面中的内容已根据 知识共享署名 4.0 许可 获得了许可,并且代码示例已根据 Apache 2.0 许可 获得了许可。内容随着时间推移,可能会过期,会定时更新或移除一些旧的内容。
最后更新时间 (CST):2023-07-20
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"很难理解"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"信息或示例代码不正确"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"没有我需要的信息/示例"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"翻译问题"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"其他"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"易于理解"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"解决了我的问题"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"其他"
}]