使用 sed,在模式上方或下方插入一行? [英] Using sed, Insert a line above or below the pattern?
本文介绍了使用 sed,在模式上方或下方插入一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要编辑大量文件,方法是在独特图案的正下方或上方插入一行或多行.请建议如何在 shell 中使用 sed
、awk
、perl
(或其他任何东西)来做到这一点.谢谢!示例:
I need to edit a good number of files, by inserting a line or multiple lines either right below a unique pattern or above it. Please advise on how to do that using sed
, awk
, perl
(or anything else) in a shell. Thanks! Example:
some text
lorem ipsum dolor sit amet
more text
我想在lorem ipsum dolor sat amet
之后插入consectetur adipiscing elit
,所以输出文件看起来像:
I want to insert consectetur adipiscing elit
after lorem ipsum dolor sit amet
, so the output file will look like:
some text
lorem ipsum dolor sit amet
consectetur adipiscing elit
more text
推荐答案
在模式后追加:(-i 用于就地替换).line1 和 line2 是您要附加(或前置)的行
To append after the pattern: (-i is for in place replace). line1 and line2 are the lines you want to append(or prepend)
sed -i '/pattern/a
line1
line2' inputfile
输出:
#cat inputfile
pattern
line1 line2
在前面添加行:
sed -i '/pattern/i
line1
line2' inputfile
输出:
#cat inputfile
line1 line2
pattern
这篇关于使用 sed,在模式上方或下方插入一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文