我有两个文件:
这是文件A的内容:
etc...
this is a test file having \@ and \# and \$ as well
looking for awk or sed solution to print this content in another file between the matching pattern
etc....
这是文件B的内容:
file-B begin
this is a large file containing many patterns like
pattern-1
pattern-2
pattern-2
pattern-3
pattern-2
pattern-4
file-B end
我想要文件 B 的输出为:
file-B begin
this is a large file containing many patterns like
pattern-1
pattern-2
pattern-2
etc...
this is a test file having \@ and \# and \$ as well
looking for awk or sed solution to print this content in another file between the matching pattern
etc....
pattern-3
pattern-2
pattern-4
file-B end
我想在文件 B 的模式 2 和模式 3 之间打印文件 A 的内容。
目前,我正在使用这个:
awk '/pattern-2/{c++;if(c==2){printf $0; print "\n"; while(getline line<"file-A"){print line};next}}1' file-B
它工作正常,但是,我需要一些东西来搜索这两个模式,然后在它们之间放置另一个文件内容。
答案1
awk -v file="file-A" '
last=="pattern-2" && $0=="pattern-3"{
while ((getline line < file)>0) print line
close(file)
}
{last=$0}1
' file-B
或者,如果您使用正则表达式模式而不是字符串,请使用类似的内容
last ~ /pattern-2/ && /pattern-3/{
在第二行。