sed基本语法
sed[options]{sed-commands}{input-file}
sed每次从input-file读取一行记录,并在该记录上执行sed-commands。
sed首先从input-file的第一行开始执行sed-commands;再读取第二行,执行sed-commands,重复这个过程,直到input-file结束。
使用sed脚本的基本语法:
sed[options] -f {sed-commands-in-a-file}{input-file}
[~/AWK_learning]$ vi test-script.sed
/^nobody/ p
/^root/ p
[~/AWK_learning]$ sed -n -f test-script.sed test.txt
nobody
root
使用-e选项,执行多个sed指令:
sed[options]-e{sed-command-1}-e{sed-command-2}{input-file}
如果使用-e执行多个命令,也可以使用反斜杠\把它们分割到多行执行:
sed -n\
-e {sed-command-1}\
-e {sed-command-2}\
input-file
也可以使用{}将多个命令分组执行:
sed[options]'{
sed-command-1
sed-command-2
}' input-file
sed 绝不会修改原始文件,它只是将结果内容输出到标准输出设备。如果要保持变更,可以使用重定向>filename.txt。