/> sed -n -e '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' -e '1,3p' testfile

    NORTHWEST       NW      CHARLES MAIN     3.0     .98     3       34
    WESTERN           WE      SHARON GRAY      5.3     .97     5       23
    SOUTHWEST       SW      LEWIS DALSASS   2.7     .8      2       18

    /> sed '2q' testfile  #打印完第二行后退出。
    northwest       NW      Charles Main    3.0     .98     3       34
    western          WE      Sharon Gray     5.3     .97     5       23

    #
当模板Lewis在某一行被匹配,替换命令首先将Lewis替换为Joseph,然后再用q退出sed
     /> sed '/Lewis/{s/Lewis/Joseph/;q;}' testfile
    northwest       NW      Charles Main      3.0     .98     3       34
    western          WE      Sharon Gray      5.3     .97     5       23
    southwest       SW      Joseph Dalsass  2.7     .8      2       18

    #
sed处理文件的时候,每一行都被保存在pattern space的临时缓冲区中。除非行被删除或者输出被取消,否则所有被处理过的行都将打印在屏幕上。接着pattern space被清空,并存入新的一行等待处理。在下面的例子中,包含模板的northeast行被找到,并被放入pattern space中,h命令将其复制并存入一个称为holding buffer的特殊缓冲区内。在第二个sed编辑命令中,当达到最后一行后,G命令告诉sedholding buffer中取得该行,然后把它放回到pattern space中,且追加到现在已经存在于模式空间的行的末尾。
     /> sed -e '/northeast/h' -e '$G' testfile
    northwest       NW     Charles Main            3.0    .98     3       34
    western          WE     Sharon Gray            5.3    .97     5       23
    southwest       SW    Lewis Dalsass          2.7     .8       2       18
    southern         SO     Suan Chin               5.1     .95     4       15
    southeast       SE      Patricia Hemenway   4.0     .7       4       17
    eastern           EA      TB Savage              4.4     .84     5       20
    northeast       NE      AM Main Jr.              5.1     .94     3       13
    north             NO      Margot Weber         4.5     .89     5       9
    central           CT      Ann Stephens          5.7     .94     5       13
    northeast       NE      AM Main Jr.              5.1     .94     3       13

    #
如果模板WE在某一行被匹配,h命令将使得该行从pattern space中复制到holding buffer中,d命令在将该行删除,因此WE匹配行没有在原来的位置被输出。第二个命令搜索CT,一旦被找到,G命令将从holding buffer中取回行,并追加到当前pattern space的行末尾。简单的说,WE所在的行被移动并追加到包含CT行的后面。
    /> sed -e '/WE/{h;d;}' -e '/CT/{G;}' testfile
    northwest       NW    Charles Main           3.0     .98     3       34
    southwest       SW    Lewis Dalsass         2.7     .8      2       18
    southern         SO     Suan Chin              5.1     .95     4       15
    southeast       SE      Patricia Hemenway   4.0     .7      4       17
    eastern           EA     TB Savage              4.4     .84     5       20
    northeast       NE      AM Main Jr.              5.1     .94     3       13
    north             NO      Margot Weber         4.5     .89     5       9
    central           CT      Ann Stephens          5.7     .94     5       13
    western         WE      Sharon Gray           5.3     .97     5       23

    #
第一个命令将匹配northeast的行从pattern space复制到holding buffer,第二个命令在读取的文件的末尾时,g命令告诉sedholding buffer中取得行,并把它放回到pattern space中,以替换已经存在于pattern space中的。简单说就是包含模板northeast的行被复制并覆盖了文件的末尾行。
    /> sed -e '/northeast/h' -e '$g' testfile
    northwest       NW     Charles Main          3.0     .98     3       34
    western          WE      Sharon Gray         5.3     .97      5       23
    southwest       SW     Lewis Dalsass        2.7     .8       2       18
    southern         SO      Suan Chin             5.1     .95     4       15
    southeast       SE      Patricia Hemenway   4.0     .7      4       17
    eastern           EA      TB Savage             4.4     .84     5       20
    northeast       NE      AM Main Jr.             5.1     .94     3       13
    north             NO      Margot Weber        4.5     .89     5       9
    northeast       NE      AM Main Jr.             5.1     .94     3       13

    #
模板WE匹配的行被h命令复制到holding buffer,再被d命令删除。结果可以看出WE的原有位置没有输出。第二个编辑命令将找到匹配CT的行,g命令将取得holding buffer中的行,并覆盖当前pattern space中的行,即匹配CT的行。简单的说,任何包含模板northeast的行都将被复制,并覆盖包含CT的行。    
    /> sed -e '/WE/{h;d;}' -e '/CT/{g;}' testfile
    northwest       NW    Charles Main           3.0     .98      3      34
    southwest       SW    Lewis Dalsass         2.7     .8       2       18
    southern         SO     Suan Chin              5.1     .95      4      15
    southeast       SE      Patricia Hemenway   4.0     .7       4      17
    eastern          EA      TB Savage              4.4     .84      5      20
    northeast       NE      AM Main Jr.              5.1     .94     3      13
    north             NO      Margot Weber        4.5     .89      5      9
    western         WE      Sharon Gray           5.3     .97     5      23

    #
第一个编辑中的h命令将匹配Patricia的行复制到holding buffer中,第二个编辑中的x命令,会将holding buffer中的文本考虑到pattern space中,而pattern space中的文本被复制到holding buffer中。因此在打印匹配Margot行的地方打印了holding buffer中的文本,即第一个命令中匹配Patricia的行文本,第三个编辑命令会将交互后的holding buffer中的文本在最后一行的后面打印出来。
     /> sed -e '/Patricia/h' -e '/Margot/x' -e '$G' testfile
    northwest       NW      Charles Main           3.0      .98      3       34
    western           WE      Sharon Gray           5.3     .97      5       23
    southwest       SW      Lewis Dalsass         2.7      .8       2       18
    southern         SO      Suan Chin               5.1      .95     4       15
    southeast       SE       Patricia Hemenway    4.0      .7       4       17
    eastern           EA      TB Savage               4.4      .84     5       20
    northeast       NE       AM Main Jr.               5.1     .94      3       13
    southeast       SE      Patricia Hemenway      4.0     .7       4       17
    central            CT      Ann Stephens            5.7     .94     5       13