打印命令(p)

打印命令(p)

Content #

打印出已有的数据文本。打印命令最常见的用法是打印包含匹配文本模式的行: $ cat data6.txt This is line number 1. This is line number 2. This is the 3rd line. This is the 4th line. $ $ sed -n ‘/3rd line/p’ data6.txt This is the 3rd line. $ 在命令行中用-n选项可以抑制其他行的输出,只打印包含匹配文本模式的行。也可以用它来快速打印数据流中的部分行: $ sed -n ‘2,3p’ data6.txt This is line number 2. This is the 3rd line. $ 如果需要在使用替换或修改命令做出改动之前查看相应的行,可以使用打印命令。来看下面的脚本: $ sed -n ‘/3/{ > p > s/line/test/p > }’ data6.txt This is the 3rd line. This is the 3rd test. $ sed编辑器命令会查找包含数字3的行,然后执行两条命令。首先,脚本用打印命令打印出原始行;然后用替换命令替换文本并通过p标志打印出替换结果。输出同时显示了原始的文本行和新的文本行。

From #

Linux命令行与shell脚本编程大全