转义反斜线时,在匹配前一行插入文件内容

编程入门 行业动态 更新时间:2024-10-23 19:34:50
本文介绍了转义反斜线时,在匹配前一行插入文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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>

更多推荐

转义反斜线时,在匹配前一行插入文件内容

本文发布于:2023-11-26 07:04:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633118.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜线   文件   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!