写入文件命令(w)

写入文件命令(w)

Content #

写入(w)命令用来向文件写入行。该命令格式如下所示:

[address]w filename

filename可以使用相对路径或绝对路径,但不管使用哪种,运行sed编辑器的用户都必须有文件的写权限。地址可以是sed支持的任意类型的寻址方式,比如单个行号、文本模式、行区间或文本模式区间。

下面的例子会将数据流中的前两行写入文本文件: $ sed ‘1,2w test.txt’ data6.txt This is line number 1. This is line number 2. This is the 3rd line. This is the 4th line. $ $ cat test.txt This is line number 1. This is line number 2. $ 当然,如果不想在STDOUT中显示文本行,可以使用sed命令的-n选项。如果要根据一些公用的文本值,从主文件(比如下面的邮件列表)中创建一份数据文件,则使用写入命令会非常方便: $ cat data12.txt Blum, R Browncoat McGuiness, A Alliance Bresnahan, C Browncoat Harken, C Alliance $ $ sed -n ‘/Browncoat/w Browncoats.txt’ data12.txt $ $ cat Browncoats.txt Blum, R Browncoat Bresnahan, C Browncoat $ sed编辑器会将匹配文本模式的数据行写入目标文件。

From #

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