검색할 헤더를 조회하기 위해 첫 번째 헤더가 소스 파일과 일치하는 경우 두 헤더 사이의 텍스트를 추출해야 합니다. 예:
&Header1
1231241241313124123213123214124123213213124124123123212
1231231231231231231231231231232131242141241241231325552
2132141241232132132132141251232132142142132132132142412
&Header2
1231241241313124123213123214124123213213124124123123212
2132141241232132132132141251232132142142132132132142412
&Header3
1231241241313124123213123214124123213213124124123123212
1231231231231231231231231231232131242141241241231325552
213214124123213213213214125123213214
그리고 내 소스 파일은 다음과 같습니다.
&Header1
&Header3
따라서 아래 번호 정보를 사용하여 헤더 1과 3만 검색하세요.
답변1
startheader=$(head -1 sourcefile)
endheader=$(tail -1 sourcefile)
# above lines assume your sourcefile has two lines in it and
# each line contains the starting header and ending header
startlinenumber=$(grep -n "${startheader}" datafile|cut -d: -f1)
endlinenumber=$(grep -n "${endheader}" datafile|cut -d: -f1)
sed -n -e "${startlinenumber},${endlinenumber}p" datafile
단일 라이너 명령을 awk
사용 하여 이 작업을 수행하는 더 정교한 방법이 있을 것이라고 확신합니다 . 그러나 저는 단지 논리를 명시적으로 제공하고 싶었을 뿐입니다. 당신은 그것을 가지고 놀 수 있고 당신의 필요에 맞게 만들 수 있습니다.perl
sed