sed中关于pattern space和hold space

24 篇文章 0 订阅
24 篇文章 0 订阅

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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anssummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值