![사전 및 사후 조건을 사용한 단락 기반 검색 UNIX KSH](https://linux55.com/image/75664/%EC%82%AC%EC%A0%84%20%EB%B0%8F%20%EC%82%AC%ED%9B%84%20%EC%A1%B0%EA%B1%B4%EC%9D%84%20%EC%82%AC%EC%9A%A9%ED%95%9C%20%EB%8B%A8%EB%9D%BD%20%EA%B8%B0%EB%B0%98%20%EA%B2%80%EC%83%89%20UNIX%20KSH.png)
다음과 같은 파일이 있습니다.
<Set of long paragraphs here>
<Set of long paragraphs here>
<Set of long paragraphs here>
After
Address1
Address2
Address3
Before
<Set of long paragraphs here>
<Set of long paragraphs here>
<Set of long paragraphs here>
After
Address1
Address2
Address3
Before
<Set of long paragraphs here>
<Set of long paragraphs here>
<Set of long paragraphs here>
After
Address1
Address2
Address3
Before
<Set of long paragraphs here>
<Set of long paragraphs here>
<Set of long paragraphs here>
After
Address1
Address2
Address3
Before
<Set of long paragraphs here>
<Set of long paragraphs here>
<Set of long paragraphs here>
After
Address1
Address2
Address3
Before
<Set of long paragraphs here>
<Set of long paragraphs here>
<Set of long paragraphs here>
After
단어 사이의 줄을 별도의 Before
출력 파일 에 작성해야 합니다 . 예를 들어 출력 파일은 다음과 같아야 합니다.
Line Number : Address1
Line Number : Address2
Line Number : Address3
......................
......................
나는 이것을 시도했습니다 :
awk '/After/,/Before/' Details.txt>output.txt
그러나 그것은 작동하지 않았습니다. 어떻게 해야 하나요?
답변1
다음과 같이 시도해 볼 수 있습니다.
$ awk '/After/{a=1;next}/Before/{a=0}; (a==1){print NR" : "$0}' file
5 : Address1
6 : Address2
7 : Address3
13 : Address1
14 : Address2
15 : Address3
21 : Address1
22 : Address2
23 : Address3
29 : Address1
30 : Address2
31 : Address3
37 : Address1
38 : Address2
39 : Address3
아이디어는 간단합니다. a
현재 줄이 After
or와 일치하면 변수의 값이 변경되고 Before
1과 같은 경우에만 줄이 인쇄됩니다.a
별도의 출력 파일에 저장하려면 다음을 수행할 수 있습니다.
awk '/After/{a=1; k++;next}/Before/{a=0}; (a==1){print NR" : "$0 > k".out"}' file
k
여기에서는 단어가 발견된 횟수를 계산하기 위해 변수를 사용합니다 After
. 출력 파일은 k
및 의 현재 값이 됩니다 .out
. 귀하의 예에서 위 코드를 실행하면 다음과 같은 결과를 얻습니다.
$ ls
1.out 2.out 3.out 4.out 5.out