sed 追加文本类容_使用sed命令在文件的两个模式之间添加文本

I want to add Some large code between two patterns:

File1.txt

This is text to be inserted into the File.

infile.txt

Some Text here

First

Second

Some Text here

I want to add File1.txt content between First and Second :

Desired Output:

Some Text here

First

This is text to be inserted into the File.

Second

Some Text here

I can search using two patterns with sed command ,But I don't have idea how do I add content between them.

sed '/First/,/Second/!d' infile

解决方案

Since /r stands for reading in a file, use:

sed '/First/r file1.txt' infile.txt

Add -i (that is, sed -i '/First/r file1.txt' infile.txt) for in-place edition.

To perform this action no matter the case of the characters, use the I mark as suggested in Use sed with ignore case while adding text before some pattern:

sed 's/first/last/Ig' file

As indicated in comments, the above solution is just printing a given string after a pattern, without taking into consideration the second pattern.

To do so, I'd go for an awk with a flag:

awk -v data="$(

Given these files:

$ cat patt_file

This is text to be inserted

$ cat file

Some Text here

First

First

Second

Some Text here

First

Bar

Let's run the command:

$ awk -v data="$(

Some Text here

First #

First

This is text to be inserted #

Second

Some Text here

First #

Bar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值