10.3.7 打印
使用p命令可进行打印,这里使用sed命令时一定要加-n参数,表示不打印没关系的行。从之前的例子中可以看出,由于sed的工作原理是基于行的,因此每次都有大量的输出。可是这些输出中有一些是我们并不需要看到的,而只需要输出匹配的行或者处理过的行就好了。简单来说,打印操作是删除操作的“逆操作”。下面使用演示来具体说明:
#
打印出文件中指定的行
[root@localhost ~]# sed -n '1p' Sed.txt this is line 1, this is First line #
将the
替换成THE
sed
实际处理了第二行,其他几行由于没有匹配所以并未真正处理
#
但是sed
的工作原理是基于流的,所以所有流过的行都打印出来了
[root@localhost ~]# sed 's/the/THE/' Sed.txt this is line 1, this is First line this is line 2, THE Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line #
使用p
命令,则只打印实际处理过的行,简化了输出(使用-n 参数)
[root@localhost ~]# sed -n 's/the/THE/p' Sed.txt this is line 2, THE Second line, Empty line followed