
두 개의 파일이 있습니다.
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/{
두 번째 줄에.