使用SED,插入下方的行(或以上)的格局? [英] Using sed, Insert a line below (or above) the pattern?

查看:128
本文介绍了使用SED,插入下方的行(或以上)的格局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编辑好多个文件,通过插入(2)无论是行正下方的独特模式或上面。请指教如何使用SED,AWK,Perl或任何壳别的事情可做了。谢谢

 一些文本
你好我的爱,你是肮脏的女孩
更多的文字

我要插入我知道,我是,我的性感骑士''你好我的爱,你是肮脏的女孩之后,这样的输出文件看起来像

 一些文本
你好我的爱,你是肮脏的女孩
我知道,我,我的性感骑士
更多的文字


解决方案

要在模式之后追加:(-i是就地替换)。一号线2号线和都行要追加(或prePEND)

  SED -i'/模式/ A \\
一号线\\
2号线'inputfile中

要prePEND行前:

  SED -i'/模式/ I \\
一号线\\
2号线'inputfile中

I need edit a good number of files, by inserting (2) lines either right below a unique pattern or above it. please advise how to do it using sed, awk, perl or anything else in shell. thanks

some text
Hello My love, you're dirty girl
more text

I want to insert 'I know, I am, my sexy knight' after ' Hello My love, you're dirty girl', so output file will look like

some text
Hello My love, you're dirty girl
I know, I am, my sexy knight
more text

解决方案

To append after the pattern: (-i is for inplace replace). line1 and line2 are the lines you want to append(or prepend)

 sed -i '/pattern/a \
line1 \
line2' inputfile

To prepend the lines before:

sed -i '/pattern/i \
line1 \
line2' inputfile

这篇关于使用SED,插入下方的行(或以上)的格局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆