使用 shell 脚本在指定模式后插入多行到文件中 [英] Insert multiple lines into a file after specified pattern using shell script

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

问题描述

我想使用 shell 脚本将多行插入到一个文件中.让我们考虑我的输入文件内容是:input.txt:

I want to insert multiple lines into a file using shell script. Let us consider my input file contents are: input.txt:

abcd
accd
cdef
line
web

现在我必须在 input.txt 文件中的cdef"行之后插入四行.插入我的文件后应该像这样改变:

Now I have to insert four lines after the line 'cdef' in the input.txt file. After inserting my file should change like this:

abcd
accd
cdef
line1
line2
line3
line4
line
web

上面的插入我应该使用shell脚本来做.有人可以帮我吗?

The above insertion I should do using the shell script. Can any one help me?

推荐答案

另一个 sed,

sed '/cdef/r add.txt' input.txt

input.txt:

abcd
accd
cdef
line
web

添加.txt:

line1
line2
line3
line4

测试:

sat:~# sed '/cdef/r add.txt' input.txt
abcd
accd
cdef
line1
line2
line3
line4
line
web

如果您想应用 input.txt 文件中的更改.然后,将 -ised 一起使用.

If you want to apply the changes in input.txt file. Then, use -i with sed.

sed -i '/cdef/r add.txt' input.txt

如果你想使用正则表达式作为表达式,你必须使用带有 sed-E 标签.

If you want to use a regex as an expression you have to use the -E tag with sed.

sed -E '/RegexPattern/r add.txt' input.txt

这篇关于使用 shell 脚本在指定模式后插入多行到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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