
У меня есть два файла:
Вот содержимое файла 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
Я хочу распечатать содержимое файла A между шаблонами 2 и 3 файла B.
В настоящее время я использую это:
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/{
во второй строке.