替代几行的文件,从另一个文件行 [英] substitute several lines in a file with lines from another file
本文介绍了替代几行的文件,从另一个文件行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个文件(A.TXT和B.txt):
I have two files (A.txt and B.txt):
A.TXT:
many lines of text
marker
A1 A2 A3
A4 A5 A6
...
... AN
many line of text
B.txt:
some text
B1 B2 B3
B4 B5 B6
...
... BN
我要顶替的A1 - 在A.TXT AN块由B1 - BN从B.txt结果
请求的结果是:
I want to substitue the A1 - AN block in A.txt by B1 - BN from B.txt.
The requested result is:
many lines of text
marker
B1 B2 B3
B4 B5 B6
...
... BN
many line of text
我知道如何找块:
I know how to find the A block:
grep -n "marker" A.txt |cut -f1 -d:
我知道如何获得对B块:
I know how to get the B block:
sed -n '2,+6p' B.txt
我甚至可以编写脚本的东西,如:
I might even be able write a script with something like:
for ...
var=$(sed -n '2p' B.txt)
sed -i "34s/.*/$var/" A.txt
但我在寻找一些简单而优雅。
But I'm looking for something simple and elegant
推荐答案
-
您可以开始越来越B.txt的过滤类常:
You can start getting the filtered constent of B.txt:
sed -n '/B1/,$p' B.txt
-
然后就可以
标记
在A.TXT用的研究
命令: Then you can append it after
marker
in A.txt with ther
command:sed "/marker/r"<(sed -n '/B1/,$p' B.txt) A.txt
-
然后你可以从
A1
直到AN
在A.TXT删除: And then you can delete from
A1
untilAN
in A.txt:sed "/A1/,/AN/d" A.txt
Altogheter
sed -e "/marker/r"<(sed -n '/B1/,$p' B.txt) -e "/A1/,/AN/d" A.txt
示例
$ sed -e "/marker/r"<(sed -n '/B1/,$p' B.txt) -e "/A1/,/AN/d" A.txt
many lines of text
marker
B1 B2 B3
B4 B5 B6
...
... BN
many line of text
这篇关于替代几行的文件,从另一个文件行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文