linux sed 用法

manual
http://www.gnu.org/software/sed/manual/sed.html

Overview

  • 使用sed的常见形式
    sed SCRIPT INPUTFILE...
  • 把文件中的‘hello’ 替换为 ‘world’
    sed 's/hello/world/' input.txt > output.txt
  • 不说明输入文件或是输入文件为横杆时候,sed将处理标准输入的内容,下面命令等价
    sed 's/hello/world/' input.txt > output.txt
    sed 's/hello/world/' < input.txt > output.txt
    cat input.txt | sed 's/hello/world/' - > output.txt

  • sed 默认输出是标准输出,用选项 -i ,把编辑的结果保存到文件,而不是把结果打印到标准输出。
    下面的命令会修改文件,没有输出。
    sed -i 's/hello/world/' file.txt
  • sed 默认打印所有要处理的输入行(除非通过d删除的行不打印),选项 -n to suppress output,用-p打印特定行,
    下面的命令打印第3行
    sed -n '45p' file.txt
  • 没有选项-e or -f 时候,sed用第一个非选项参数作为脚本例如('s/hello/world/'),接着的非选项参数作为输入文件。
    如果有选项-e or -f 来指定脚本,所有的非选项参数作为输入文件。-e and -f 可以同时出现多次。
    The following examples are equivalent:
    sed 's/hello/world/' input.txt > output.txt
    sed -e 's/hello/world/' input.txt > output.txt
    sed --expression='s/hello/world/' input.txt > output.txt
    echo 's/hello/world/' > myscript.sed
    sed -f myscript.sed input.txt > output.txt
    sed --file=myscript.sed input.txt > output.txt

命令行选项 Command-Line Options

  • The full format for invoking sed is:
    sed OPTIONS... [SCRIPT] [INPUTFILE...]

猜你喜欢

转载自www.cnblogs.com/ims-/p/10421528.html
今日推荐