
たとえば、次を含むファイルがあるとします。
Hello world
Hello earth
Hi everybody
Hello
以下を使用して簡単に解析し、単語を含む行の最初の列と2番目の列を取得できます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
2 つのファイルを使用できます。
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