10. 写标志(w 标志)
sed 替换标志 w
代表写入。替换成功后,会将更改的行写入文件。大多数人使用 p
标志,并将输出重定向到文件。
为了教程完整性,我们包含对此命令的说明。
写入示例
以下是几个简单的写入示例。
Example1:仅将替换命令更改的行写入到文件
$ sed -n 's/John/Johnny/w output.txt' employee.txt $ cat output.txt 101,Johnny Doe,CEO
Example2:将 "locate"
的第二个实例更改为 "find"
,将结果写入文件并打印所有行
正如我们在p
命令中展示的那样,将w
添加到带有substitute-locate.txt
文件的示例中会将更改的两行发送到输出文件。
$ sed 's/locate/find/2w output.txt' substitute-locate.txt
locate command is used to find files
locate command uses database to find files
locate command can also use regex for searching
$ cat output.txt
locate command is used to find files
locate command uses database to find files