
我有兩個文件:
這是文件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/{
在第二行。