sed中关于pattern space和hold space

来源:互联网 发布:linux系统各种下载 编辑:程序博客网 时间:2024/04/27 12:54

sed的用法是: sed OPTIONS... [SCRIPT] [FILE...]

简单说下sed的工作流程。pattern space和hold space默认都是空的。sed读入一行内容,删除尾部的换行符,存入pattern space, 然后执行SCRIPT,如果OPTIONS里没有 -n, pattern space里的内容会被输出到stdout(若读入时含有换行,这里会输出换行), 如果有-n, 则不输出,接下来就读取下一行数据,重复上述步骤。

再说一下一会要在SCRIPT中用到的几个命令。
d : 清空pattern space中的内容,立即开始下个循环(意思是跳过默认的输出pattern space内容的阶段???不知理解的对不对)
h : 用pattern space中的内容替换hold pattern中的内容
H : 在hold space中的内容后面追加一个换行,把pattern space中的内容追加到hold space中
g : 用hold space中的内容替换pattern space中的内容
G : 在pattern space中的内容后面追加一个换行,把hold space中的内容追加到pattern space中

h, g会替换(可以理解为先清空,再复制), H, G是追加。


这个例子是这样的,有一个文件myfile.txt, 内容如下:

This is the 1st line.2nd line is here.i'm the 3rd.why i am the last one?

 

期望逆序输出其内容,也就是如下效果

why i am the last one?i'm the 3rd.2nd line is here.This is the 1st line.

 

解法如下:

$ sed '1!G;h;$!d' myfile.txt

 

执行步骤:
首先读取myfile.txt的第一行到pattern space,如下所示:

    pattern space                    hold space     ------------                    ------------    This is the 1st line.            <null>

 

执行命令'1!G;h;$!d', 由于是第一行,所以'1!G'被跳过,执行'h', 用pattern space中的内容替换hold space中的内容,由于不是最后一行,执行'$!d'清空pattern space,此时状态是:

     pattern space                    hold space     ------------                    ------------    <null>                            This is the 1st line.

 

读取第二行到pattern space

     pattern space                    hold space     ------------                    ------------    2nd line is here.                This is the 1st line.

 

已经不是第一行了,所以执行'1!G', hold space中的内容追加都pattern space

     pattern space                    hold space     ------------                    ------------    2nd line is here.                This is the 1st line.    This is the 1st line.

 

执行'h', pattern space的内容替换hold space

     pattern space                    hold space     ------------                    ------------    2nd line is here.                2nd line is here.    This is the 1st line.            This is the 1st line.

 

执行'$!d', 清空pattern space, 读取第三行到pattern space

     pattern space                    hold space     ------------                  ------------    i’m the 3rd.                    2nd line is here.                                     This is the 1st line.

 

执行'1!G', hold space中的内容追加都pattern space

     pattern space                    hold space     ------------                  ------------    i’m the 3rd.                     2nd line is here.     2nd line is here.                 This is the 1st line.    This is the 1st line.

 

执行'h', pattern space的内容替换hold space

     pattern space                    hold space     ------------                  ------------    i’m the 3rd.                     i’m the 3rd.     2nd line is here.                 2nd line is here.     This is the 1st line.            This is the 1st line.

 

执行'$!d', 清空pattern space, 读取第四行到pattern space

     pattern space                    hold space     ------------                  ------------    why i am the last one?            i’m the 3rd.                                     2nd line is here.                                     This is the 1st line.

 

执行'1!G', hold space中的内容追加都pattern space

复制代码
     pattern space                    hold space     ------------                  ------------    why i am the last one?     i’m the 3rd.                     i’m the 3rd.     2nd line is here.                 2nd line is here.     This is the 1st line.            This is the 1st line.
复制代码

 

执行'h', pattern space的内容替换hold space

复制代码
     pattern space                    hold space     ------------                  ------------    why i am the last one?             why i am the last one?     i’m the 3rd.                     i’m the 3rd.     2nd line is here.                 2nd line is here.     This is the 1st line.            This is the 1st line.
复制代码

 

现在已经是最后一行,'$!d'将不会被执行,所有SCRIPT执行完之后,输出pattern space中的内容

why i am the last one?i'm the 3rd.2nd line is here.This is the 1st line.

 

得到最终结果。

根据info sed,默认情况下,pattern space中的内容会在两次循环之间(输出之后,读取之前)被清空; 而hold space则不会自动清空。

----------------------------------
这个命令还有一种写法

$ sed -n '1!G;h;$p' myfile.txt

 

这里用了-n选项,如前面所说,它会跳过SCRIPT执行完后,pattern space的输出动作,而'$p'则是的执行到最后一行的时候,执行'p'来强制输出最终结果。

0 0
原创粉丝点击