sed 命令簡介

  1. sed 命令輔助說明如下:
    [dywang@dywmac zzz]$ sed --help
    Usage: 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 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
                     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
          --help     display this help and exit
          --version  output version information and exit
    If no -e, --expression, -f, or --file option is given, then the first
    non-option argument is taken as the sed script to interpret.  All
    remaining arguments are names of input files; if no input files are
    specified, then the standard input is read.
    GNU sed home page: <>.
    General help using GNU software: <>.
    E-mail bug reports to: <>.
    Be sure to include the word “sed” somewhere in the “Subject:” field.
  2. sed (stream editor) 可以分析 Standard Input (STDIN) 的資料,進行取代、刪除、新增、擷取特定行等處理後,再輸出到 standrad out (STDOUT),功能簡介如下:
    [root@linux ~]# sed [-nefr] [動作]
    -n  :使用安靜 (silent) 模式。在一般 sed 的用法中,所有來自 STDIN 
          的資料一般都會被列出到螢幕上。但如果加上 -n 參數後,則只有經過
          sed 特殊處理的那一行(或者動作)才會被列出來。
    -e  :直接在指令列模式上進行 sed 的動作編輯;
    -f  :-f filename 可以執行 filename 內的 sed 動作;
    -r  :sed 的動作支援的是延伸型正規表示法的語法。(預設是基礎正規表示法語法)
    -i  :直接修改讀取的檔案內容,而不是由螢幕輸出。
    動作說明:  [n1[,n2]]function
    n1, n2 :選擇進行動作的行數,例如,『10,20[動作行為] 』
    a   :新增, a 的後面可以接字串,這些字串會在目前的下一行出現。
    c   :取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行。
    d   :刪除, d 後面通常不接任何字串;
    i   :插入, i 的後面可以接字串,這些字串會在目前的上一行出現;
    p   :列印,將某個選擇的資料印出。通常 p 會與參數 sed -n 一起運作。
    s   :取代,s 的動作可以搭配正規表示法。例如 1,20s/old/new/g 。