sed 基本语法:
sed options ‘commands’ file
sed 首先从 file 中读取第一行,然后执行所有的 commands;
再读取第二行,执行 所有 sed-commands;
重复这个过程,直到 input-file 结束 。
(options是可选项,command必须加,如果直接sed file 会报missing commands,commands外面的’'可以不加)
commands可以为一个或多个
p : 打印command
一个是最简单形式,如:
sed -n p file:打印file所有内容到标准输出
多个稍微复杂,以两个为例,可以为command1&commmand2的与形式,也可以为command1 | command的或形式,如:
sed -n ‘/hi/p’ file :打印file中的匹配到hi的行
sed -n -e ‘/hi/p’ -e ‘/hello/p’ :打印file中匹配到hi或者hello的行
当commands很多时,可以用{}将所有commands包住,一行一个命令:
sed -n ‘{
/hi/p
/hello/p
/world/p
}’
当comands很多,而且有时需要后期修改时,上面更实用的方法是,将所有的commands写入文件file1中,用 -f调用文件:
此时 sed语法变为:
sed options -f file1 file
sed 内部执行过程查看另一篇博文&#