`
icepp
  • 浏览: 10394 次
  • 性别: Icon_minigender_1
  • 来自: 大连
最近访客 更多访客>>
社区版块
存档分类
最新评论

对sed1line中一些命令的理解 二

阅读更多

 

# insert a blank line above every line which matches "regex" 
#在每个含有字符串regex的行上插入一行空白行 
sed '/regex/{x;p;x;}'

 

复习下x ,p命令
x   Exchange the contents of the hold and pattern spaces.
P   Print the current pattern space.

 

脚本工作流程:


当读入匹配RE的那一行时,
      pattern space:  regex
      hold space :  blank


先执行x,此时
      pattern space: blank
      hold space : regex


继续执行p,空行被打印出来
      pattern space: blank
      hold space : regex


往下执行x
      pattern space: regex
      hold space : blank


到脚本底,执行默认输出regex,读入下行继续处理。

这样就在每个含有字符串regex的行上插入一行空白行。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics