sed 编辑器的选项用法浅谈

sed 在linux下作文本转换,过滤之用,使用范围比较广泛。

最近重温一些参数用法,理解起来有些费劲。

: label
       Label for b and t commands.

  lable 可以配合  b , t ,T 命令使用

下面详细讲解配合的用法:

  1,建一个测试的文本

      echo -e "1111111\n222222\n333333\n444444\n555555\n666666" > sed.txt

    默认sed 每次加载一行到模式空间(pattern space)

    n N    Read/append the next line of input into the pattern space.

   N 命令会同时加载下一行到模式空间

    执行 sed 'N;s/\n//' sed.txt  每两行会连接为一行输出

  2,label  配合 b 命令使用

    执行:sed ':token;N;s/\n//;b token' sed.txt 

    输出如下:

      1111111222222333333444444555555666666

     sed命令第一次执行 1和2 之间的换行符被替换,b 命令重新回到 label:token 再次执行命令 ,3 这行被加载进模式空间,执行替换命令,sed 循环执行命令,直到所有的文本行加载执行完毕。

     这种执行方式,没有条件限制,每次b命令都会执行 label 这一步。t,T 则是有条件执行

   3,label  配合 t,T命令使用

    摘出来一段文档里面的解释:

    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 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.  This is a
              GNU extension.

    t命令 在 s/// 有替换操作 则 执行label,命令进入循环状态,模式空间数据保留,再次执行label后的命令

    执行:sed ':token;N;s/\n//;t token' sed.txt

    输出如下:

      1111111222222333333444444555555666666

    T命令 在 s/// 没有替换操作 则 执行label,命令进入循环状态,模式空间数据保留,再次执行label后的命令

    执行: sed ':token;N;s/\n//;T token' sed.txt

    输出如下:

      1111111222222
      333333444444
      555555666666

    

  -------------------------------------END-----------------------------------------

  

      

 

      

    

 

  

转载于:https://www.cnblogs.com/iszhihui/p/5828105.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值