我有两份文件:
这是文件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它工作得很好,但是,我需要一些东西来搜索这两种模式,然后在它们之间放置另一个文件内容。
发布于 2020-03-20 05:14:13
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或者,如果您使用regex模式而不是字符串,请使用以下内容
last ~ /pattern-2/ && /pattern-3/{在第二行。
https://unix.stackexchange.com/questions/573871
复制相似问题