10.3.8 写文件
正如之前所说,sed本身默认并不改写原文件,而只是对缓冲区的文本做了修改并输出到屏幕。所以想保存文件,除了之前提到的两种方法外(使用重定向或-i参数),还可以使用w命令将结果保存到外部指定文件。示例如下:
- [root@localhost ~]# sed -n '1,2 w output' Sed.txt [root@localhost ~]# #
这里没有任何输出,因为输出被重定向到文件了
#
文件output
中的内容正是Sed.txt 文件中前两行的内容
[root@localhost ~]# cat output this is line 1, this is First line this is line 2, the Second line, Empty line followed