10. 写标志(w 标志)


sed 替换标志 w 代表写入。替换成功后,会将更改的行写入文件。大多数人使用 p 标志,并将输出重定向到文件。 为了教程完整性,我们包含对此命令的说明。

以下是几个简单的写入示例。

$ sed -n 's/John/Johnny/w output.txt' employee.txt

$ cat output.txt
101,Johnny Doe,CEO

正如我们在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