【问题标题】:Inserting multiline text from file to another file after pattern在模式之后将多行文本从文件插入到另一个文件
【发布时间】:2011-02-22 09:26:52
【问题描述】:

我有类似的文件:

文本2插入
带模式的文件

又名:

模式

如何将text2insert 中的行插入filewithpattern 但在pattern 行之后?

使用 bash 2.05b

更新: filewithpattern 之前应该是这样的:

垃圾
图案1
图案2
垃圾

之后:

垃圾
图案1
text2插入行
text2插入行
text2插入行
图案2
垃圾

【问题讨论】:

  • 你能说得更具体点吗?比如pattern之后的内容是否被删除?

标签: bash


【解决方案1】:
sed -e '/pattern/r text2insert' filewithpattern

【讨论】:

  • 一切如我所愿。它简短而简单。我非常喜欢。谢谢丹尼斯!
  • 有没有类似的脚本来插入text2insert的内容上面pattern匹配的行?
【解决方案2】:
awk 'FNR==NR{      a[c++]=$0;next     }
/pattern1/{g=1;next}
/pattern2/{g=0;next}
g{ 
  for(i=1;i<=c;i++){
    print a[i]
  }
}' text2insert filewithpattern

为什么在输出中需要“垃圾”?您之前的问题似乎不包括“垃圾”

【讨论】:

  • 最后一行应该是这样的吧?我的意思是...file 是什么??
  • @kasper, filefilewithpattern
  • 好吧,垃圾只是为了说明脚本不应该触及的前后模式。
猜你喜欢
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 2011-04-16
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 2020-03-19
相关资源
最近更新 更多