![awk를 사용하여 지속적으로 변경되는 파일을 구문 분석하는 방법은 무엇입니까?](https://linux55.com/image/37479/awk%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%EC%A7%80%EC%86%8D%EC%A0%81%EC%9C%BC%EB%A1%9C%20%EB%B3%80%EA%B2%BD%EB%90%98%EB%8A%94%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%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
예를 들어 다음을 포함하는 파일이 있는 경우:
Hello world
Hello earth
Hi everybody
Hello
다음을 사용하여 이를 쉽게 구문 분석하여 단어가 포함된 행의 첫 번째 열과 두 번째 열을 얻을 수 있습니다 awk
.
awk '/Hello/ { print $1 }' file
awk '/Hello/ { print $2 }' file
파일 내용이 변경된 경우 수행할 작업:
Hello world
Hello earth
Hi everybody
Hello sky
Hi madame
Hello USA
awk
(를 사용하여 ) 이 파일에 추가된 새 항목만 구문 분석하려면 어떻게 해야 합니까 ?
Hello sky
Hi madame
Hello USA
이미 파싱된 정보를 다시 파싱할 필요가 없나요?
Hello world
Hello earth
Hi everybody
답변1
두 가지 파일을 사용할 수 있습니다.
if [ -e checkfile ]; then
lines=$(wc -l <checkfile)
else
lines=0
fi
# read all new lines from source file and append them to target file
sed -n $((lines+1)),\$p file >>checkfile
awk '...' checkfile
답변2
tail -f
tail -F
예를 들어 (또는)을 통해 파일의 내용을 파이프할 수 있습니다 .
tail -f file | awk '...'
tail
매뉴얼 페이지 에서 :
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --fol‐
low=descriptor are equivalent