Sed学习实录

来源:互联网 发布:田宫四驱车淘宝 编辑:程序博客网 时间:2024/05/14 21:36

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.txtnobodyroot

  使用-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-1sed-command-2}' input-file

  sed 绝不会修改原始文件,它只是将结果内容输出到标准输出设备。如果要保持变更,可以使用重定向>filename.txt。

原创粉丝点击