ITeye博客

对sed1line中一些命令的理解 二

icepp · 2009-03-01 10:54 · Shell · Markdown

# 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的行上插入一行空白行。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。