使用Shell脚本在指定的模式后将多行插入文件 [英] Insert multiple lines into a file after specified pattern using shell script
问题描述
我想使用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:
input.txt:
abcd
accd
cdef
line
web
add.txt:
line1
line2
line3
line4
测试:
sat:~# sed '/cdef/r add.txt' input.txt
abcd
accd
cdef
line1
line2
line3
line4
line
web
如果要在input.txt
文件中应用更改.然后,将-i
与sed
一起使用.
If you want to apply the changes in input.txt
file. Then, use -i
with sed
.
sed -i '/cdef/r add.txt' input.txt
如果要使用正则表达式作为表达式,则必须将-E
标记与sed
一起使用.
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屋!