使用 sed 在模式之前将文件内容插入到文件中 [英] using sed to insert file content into a file BEFORE a pattern

查看:34
本文介绍了使用 sed 在模式之前将文件内容插入到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 sed 的新手,我正在尝试将 BFile 的内容插入到文件 AFile 中,在模式之前(在 AFile 中)

I'm new to sed, I'm trying to insert the content of a BFile into the file AFile, BEFORE a pattern (in AFile)

以下是我尝试过的:

sed -i '/blah Blah/r BFile' AFile :它在 AFile 中的模式之后插入 BFile 的内容.

sed -i '/blah Blah/r BFile' AFile : it's inserting the content of BFile AFTER the pattern in AFile.

sed -i '/blah Blah/i BFile' AFile :它在 AFile 中的模式之前插入 string 'BFile'.

sed -i '/blah Blah/i BFile' AFile : it's inserting the string 'BFile' BEFORE the pattern in AFile.

...嗯

我意识到这是因为对 regexp 或 sed 的错误理解:我无法理解 /i/r 在这里工作...我在 sed --help

I'm conscious it's because of a wrong comprehension of regexp or sed : I can't understand how the /i and /r work here... I can't find any help in sed --help

有人明白我的意思吗?

问候,

斯坦

推荐答案

这可能对你有用(GNU sed):

This might work for you (GNU sed):

sed $'/blah Blah/{e cat BFile\n}' AFile

或:

sed -e 'N;/\n.*blah Blah/{r BFile' -e '};P;D' AFile

或者正如 Alek 指出的那样:

or as Alek pointed out:

sed '/blah Blah/e cat BFile' AFile

这篇关于使用 sed 在模式之前将文件内容插入到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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