다음 prova.txt과 같은 파일 이 있습니다.
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
extra1
extra2
bla
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
extra2
bla
bla
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
"여기서 시작하십시오"에서 첫 번째 빈 줄로 grep해야합니다. 출력은 다음과 같아야합니다.
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
보시다시피 "여기서 시작하기"이후의 줄은 무작위이므로 -A -B grep 플래그가 작동하지 않습니다.
cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt
빈 줄까지 잡을 첫 번째 줄을 잡는 방법 ( "여기서부터 시작"으로)을 찾는 방법을 알려 주시겠습니까? "여기서 시작하기"이후에 임의의 라인이 몇 줄인지 예측할 수 없습니다.
유닉스 호환 솔루션은 인정합니다 (grep, sed, awk가 perl 또는 유사한 것보다 낫습니다).
편집 : @ john1024의 화려한 응답 후 가능한지 알고 싶습니다.
1 ° 블록 정렬 (여기서 시작 시작에 따라 : 1, 1, 2)
2 ° 4 개 (알파벳으로 임의의) 행을 제거합니다. fix1, fix2, fix3, fix4 그러나 항상 4입니다.
3 °는 sort -u 명령과 같은 임의의 속임수를 제거합니다.
최종 출력은 다음과 같습니다.
# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4
#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
또는
# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
두 번째 출력은 첫 번째 출력보다 낫습니다. 다른 유닉스 명령 마법이 필요합니다.