转义反斜线时,在匹配前一行插入文件内容 [英] Insert File Contents One Line Before Match While Escaping Backslash

查看:84
本文介绍了转义反斜线时,在匹配前一行插入文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了类似的问题如何在换行符后转义反斜杠时插入文件内容只是为了意识到我需要做相反的事情.

I recently asked a similar question How To Insert File Contents After Line Match While Escaping Backslash only to realize I need to do the opposite.

使用该问题中的代码,我尝试过:

Using the code from that question, I have tried:

sed '/<\\tag>/i file2' file1

第二次尝试:

awk '/<\\tag>/{print;system("cat File2.txt");before} 1' File1.txt

但是,我无法让它在比赛之前插入一行.

However, I'm unable to get it to insert one line before the match.

示例:

File1.txt

File1.txt

Hello <[World!]> 1
Hello <[World!]> 2

File2.txt:

File2.txt:

<thanks>
<tag>
<example>
<new>
<\new>
<\example>
<\tag>
<\thanks>

所需的输出:

<thanks>
<tag>
<example>
<new>
<\new>
<\example>
Hello <[World!]> 1
Hello <[World!]> 2
<\tag>
<\thanks>

如果您可以帮助我在转义后插入文件内容,同时转义反斜杠,我将不胜感激.

If you could please help me insert the file contents after a line match while escaping a backslash, I would really appreciate it.

推荐答案

$ awk 'FNR==NR{n=n $0 ORS; next} /<\\tag>/{$0=n $0} 1' file1 file2
<thanks>
<tag>
<example>
<new>
<\new>
<\example>
Hello <[World!]> 1
Hello <[World!]> 2
<\tag>
<\thanks>

这篇关于转义反斜线时,在匹配前一行插入文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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