歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> sed流式編輯器

sed流式編輯器

日期:2017/3/1 16:05:00   编辑:關於Linux
sed流式編輯器 sed是一個流式編輯器(stream editor)。 在sed中,pattern space和hold space為兩個緩沖區,開始時數據由標准輸入被讀入pattern space,經過處理輸出到標准輸出。hold space只是一個輔助處理的緩沖區,有些命令可以對hold space緩沖區操作,比如H,h,g,G等。 sed [OPTION]... {script-only-if-no-other-script} [input-file]... 這是命令的標准格式: 描述:sed是一個流式編輯器,流式編輯器用來處理對一個輸入 流進行處理。就像很多運行腳本編輯的編輯器一樣,sed只處理一行輸入,因此更高效。但是sed在管道中過濾文本的作用使它明顯區別於其他類型編輯器。 -n, --quiet, --silent 抑制pattern space的自動輸出功能 -e script, --expression=script 把script加入到執行命令中 -f script-file, --file=script-file 命令輸入來源為文件 -i[SUFFIX], --in-place[=SUFFIX] 原地編輯 -l N, --line-length=N 設定自動換行的長度 --posix 關閉所有GNU擴展 -r, --regexp-extended 應用擴展的正則表達式 -s, --separate 把多個文件當做多個流 -u, --unbuffered 每次從文件讀入更少數據更頻繁的flush到輸出 如果沒有-e 或者-f選項,第一個非選項參數作為sed腳本運行。其余所有的參數都為輸入文件,如果沒有輸入文件,從標准輸入讀取。 COMMAND SYNOPSIS 這只是一個sed的簡明教程 零地址命令: :label 為 b 和 t命令用的label #comment 直到下一行命令都為注釋(或者-e 命令的末尾) 零或者一個地址命令: = 打印當前行號 a text: 加入text,可以用反斜槓換行 i text: 插入text,可以用反斜槓換行 q 立即退出sed腳本不處理任何更多的輸入,但如果沒有禁用自動打印,將打印當前模式空間。 Q 立即退出sed腳本不處理任何更多的輸入 r filename 加入從filename讀入的text R filename 加入從filaname讀入一行數據 Commands which accept address ranges b label Branch to label; if label is omitted, branch to end of script. 跳轉到label執行,如果沒有就到最後 t label If a s/// has done a successful substitution since the last input line was read and since the last t or T command, then branch to label; if label is omitted, branch to end of script. 自從上次輸入或者上次T/t命令後s替換命令執行成功,跳轉到label,沒喲label,到最後 T label If no s/// has done a successful substitution since the last input line was read and since the last t or T command, then branch to label; if label is omitted, branch to end of script. 自從上次輸入或者上次T/t命令後s替換命令執行不成功,跳轉到label,沒喲label,到最後 c text: 替換text,可以用反斜槓換行 d 從下一個循環開始刪除pattern space D刪除pattern space的第一行,如果其中有數據繼續處理,否則從讀取輸入 h H Copy/append pattern space to hold space. g G Copy/append hold space to pattern space. x Exchange the contents of the hold and pattern spaces. l List out the current line in a ''visually unambiguous'' form. 明確列出當前行號 n N Read/append the next line of input into the pattern space. p Print the current pattern space. P Print up to the first embedded newline of the current pattern space. w filename Write the current pattern space to filename. W filename Write the first line of the current pattern space to filename. s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that por- tion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp. 用replacement替換regexp匹配的字段,replacement中可能包含&字符代指與regexp匹配的字符串,用\1到\9代指相應匹配的子段 y/source/dest/ Transliterate the characters in the pattern space which appear in source to the cor- responding character in dest. Addresses 如果一個地址參數也沒有,對所有行進行處理,如果有一個地址,對哪一行進行處理,如果有兩個地址,對兩行之間的行處理,哪怕參數2小於參數1,如果地址2是一個正則表達式,不會匹配第一個參數行。 前面加!符號,反向選擇 number 具體一行 first~step 從first行開始的步長為step的行 $ 最後一行 /regexp/ 與此正則表達式匹配的一行 \cregexpc c可以為任何字符,同上,只不過把換一種形式 addr1,+N 從addr1開始的N行 addr1,~N 從addr1開始的N的整數倍行
Copyright © Linux教程網 All Rights Reserved