特定的文本后,插入文本文件 [英] Insert text into file after a specific text

查看:135
本文介绍了特定的文本后,插入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个特定的文本后的输出插入到一个文件中...下一行。

I would like to insert an output into a file...in the next line after a specific text.

在我的输出(Text1.txt)我有不同的线路用域名:

In my output (Text1.txt) I have different lines with domain names:

hxxp://example1.com
hxxp://example2.com

我想与文本发现的网址的行之后插入该行到另一个文本文件(Text2.txt)(注:我不知道的行号)

I would like to insert this lines into another text file (Text2.txt) after the line with the text "URLs found" (Note: I don't know the line number).

bla
bla
bla
URLs found
hxxp://example1.com
hxxp://example2.com
bla
bla

我怎样才能做到这一点?

How can I do this?

推荐答案

您使用能做到这一点SED:

You could do this using sed:

sed -i '/URLs found/r Text1.txt' Text2.txt

当的图案匹配时,插入Text1.txt的内容。在 -i 开关意味着SED编辑就地文件。

When the pattern is matched, insert the contents of Text1.txt. The -i switch means that sed edits the file in-place.

另外,你可以做同样的事情在AWK:

Alternatively, you could do the same thing in awk:

awk 'NR==FNR{a[n++]=$0;next} 1; /URLs found/{for (i=0;i<n;++i) print a[i]}' Text1.txt Text2.txt > tmp && mv tmp Text2.txt

阅读所有Text1.txt行到一个数组。在 1 意味着Text2.txt每行打印。当模式匹配时,数组的内容也被印刷

Read all of the lines in Text1.txt into an array. the 1; means that every line in Text2.txt is printed. When the pattern is matched, the contents of the array are also printed.

这篇关于特定的文本后,插入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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