天天看點

sed - pattern space, hold space

最近看 linux 的 sed 指令,講有到 pattern space 、hold space ,編輯指令 h, x, G 有點難以了解,研究了下得出下面的東西:

sed 有兩種緩沖區:

    通常說的 sed 将輸入檔案複制到緩沖區,對緩沖區中的副本處理後,将其輸出,這個緩沖區叫: Pattern Buffer ( pattern space );

    還有一個就是 Hold Buffer ( hold space ).

在例子之前先看看 sed 是如何工作的,

How sed Works

sed maintains two data buffers: 

    the active pattern space, and the auxiliary hold space. Both are initially empty.

sed operates by performing the following cycle on each line of input: 

    first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. 

    Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.

    When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line.

翻譯後(關于trailing newline 的部分不是很明白)大概意思如下:

1、從輸入流中讀取一行;

2、假如比對的話執行指令;

3、如果沒有 -n 選項的話,将 pattern space 中的内容列印到輸出流。

4、下一行重複1-3.

man sed 中對 h/H , g/G ,x 的描述:

h/H:copy/append pattern space to hold space. (複制/追加 pattern space 到 hold space) 

g/G:copy/append hold space to pattern space. (複制/追加 hold space 到 pattern space)

x: exchange the contents of the hold and pattern space. (互換 pattern ,hold space)

例子來了:

#cat name.txt

    1 tom

    2 jerry

    3 selina

    4 green

    5 lily

    6 lilei

#sed -e '/tom/h' -e '/green/x' -e '$G' name.txt

逐行解析如下:

文章逐行 COMMAND PATTERN SPACE HOLD SPACE OUTPUT
1 tom /tom/
h
2 jerry
3 selina
4 green /green/
x
5 lily
6 lilei $
G

是以,指令的輸出為:

最後看看 -n 選項 和 p 指令:

  • -n(

    --quiet,--silent)
  • By default, sed prints out the pattern space at the end of each cycle through the script. These options disable this automatic printing, and sed only produces output when explicitly told to via the 

    p

     command.

    p:

  • Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.  

大概意思就是, -n 選項抑制 pattern space 的自動列印,而 p 使 -n 失效。

#sed -n '/selina/p' name.txt