![텍스트 파일에서 줄을 추출하면 파일을 구문 분석하여 패턴이 식별됩니다.](https://linux55.com/image/94960/%ED%85%8D%EC%8A%A4%ED%8A%B8%20%ED%8C%8C%EC%9D%BC%EC%97%90%EC%84%9C%20%EC%A4%84%EC%9D%84%20%EC%B6%94%EC%B6%9C%ED%95%98%EB%A9%B4%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EA%B5%AC%EB%AC%B8%20%EB%B6%84%EC%84%9D%ED%95%98%EC%97%AC%20%ED%8C%A8%ED%84%B4%EC%9D%B4%20%EC%8B%9D%EB%B3%84%EB%90%A9%EB%8B%88%EB%8B%A4..png)
다음 시나리오에 따라 txt 파일에서 다른 파일로 줄을 추출해야 합니다.
일치하는 첫 번째 패턴은 항상 단어로 시작하는 줄이 되고 identifier
, 두 번째 패턴은 다음으로 시작하는 첫 번째 줄이 됩니다 000
.
아래 예에서는
000: thislineneedstobeextracted
출력을 첫 번째 파일에 쓴 후 단어로 시작하는 줄 identifier
과 다음으로 시작하는 줄을 다시 추출합니다. 000
그러나 이번에는 파일에서 두 번째로 고유한 항목이 발견된 경우 다음 예에서
000: alsothislineneedstobeextracted
출력은 두 번째 파일에 기록됩니다.
입력 예
identifier 767
linetobeignored
anotherlinetobeignored
000: thislineneedstobeextracted
000: alsothislineneedstobeextracted
blankline
identifier 7686
linetobeignored
anotherlinetobeignored
000: thislineneedstobeextracted
000: alsothislineneedstobeextracted
000: ayetanotherlineneedstobeextracted
출력 file1.txt 예:
000: thislineneedstobeextracted
identifier 767
identifier 7686
예제 출력 file2.txt:
000: alsothislineneedstobeextracted
identifier 767
identifier 7686
답변1
사용해 보세요앗
awk '
/^id/{
if(line[1]){
for(n in line)print line[n],i,$0 >> "file"n".txt"
delete(line)
}
c=0
i=$0
}
/^000:/{
line[++c]=$0
}
END{
if(line[1]){
for(n in line)print line[n],i,$0 >> "file"n".txt"
}
' OFS=\\n txt.file