Linux之sed用法

Linux.png

简介

Linux sed命令是利用script来处理、编辑文本文件。

语法

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

参数说明

  • -n, –quiet, –silent
    ​ suppress automatic printing of pattern space
  • -e script, –expression=script
    ​ add the script to the commands to be executed
  • -f script-file, –file=script-file
    ​ add the contents of script-file to the commands to be executed

    –follow-symlinks
    ​ follow symlinks when processing in place; hard links
    ​ will still be broken.

  • -i[SUFFIX], –in-place[=SUFFIX]
    ​ edit files in place (makes backup if extension supplied).
    ​ The default operation mode is to break symbolic and hard links.
    ​ This can be changed with –follow-symlinks and –copy.
  • -c, –copy
    ​ use copy instead of rename when shuffling files in -i mode.
    ​ While this will avoid breaking links (symbolic or hard), the
    ​ resulting editing operation is not atomic. This is rarely
    ​ the desired mode; –follow-symlinks is usually enough, and
    ​ it is both faster and more secure.
  • -l N, –line-length=N
    ​ specify the desired line-wrap length for the `l’ command
    –posix
    ​ disable all GNU extensions.
  • -r, –regexp-extended
    ​ use extended regular expressions in the script.
  • -s, –separate
    ​ consider files as separate rather than as a single continuous
    ​ long stream.
  • -u, –unbuffered
    ​ load minimal amounts of data from the input files and flush
    ​ the output buffers more often

实例

查看某段时间的服务日志:sed -n '/1970-01-01 01:01/,$p' 1.log |more