で大きなファイルを解析するとき、私は同じ行に2つのパターンの最後の出現を取得するためにmail.logファイルを解析したいと思い、解析されるファイルは500メガバイト& 1ギガバイトパフォーマンスの問題は、awkの
の間のサイズを有しますは、私はそれを得るために管理:
$ time awk ' $5~"postfix/error" && $6~"4F0A73A11CF" ' MAIL-POSTFIX-LOG-20160226.log |
tail -1
Feb 26 21:49:23 smtp1 postfix/error[32347]: 4F0A73A11CF: to=<[email protected]>,
relay=none, delay=88661, delays=88661/0.02/0/0.05, dsn=4.4.1, status=deferred (delivery
temporarily suspended: connect to xxxxxxxxxxxxxxxxxxxx[x.x.x.x]:25: Connection timed out)
real 0m3.572s
user 0m1.920s
sys 0m1.600s
私はawkコマンドを使用して維持したいと思いますが、私は劇的データの数日間を解析するためのパフォーマンスを改善する必要があります。
は、最後の1で始まる、ファイルを逆にTACコマンドを使用することにより、私は、grepコマンドで改善された性能を観察:
$ time tac MAIL-POSTFIX-LOG-20160226.log | grep "postfix/error" | grep -m1 "4F0A73A11CF"
Feb 26 21:49:23 smtp1 postfix/error[32347]: 4F0A73A11CF: to=<[email protected]>,
relay=none, delay=88661, delays=88661/0.02/0/0.05, dsn=4.4.1, status=deferred (delivery
temporarily suspended: connect to xxxxxxxxxxxxxxxxxxxx[x.x.x.x]:25: Connection timed out)
real 0m0.026s
user 0m0.008s
sys 0m0.016s
$ time cat MAIL-POSTFIX-LOG-20160226.log | grep "postfix/error" | grep "4F0A73A11CF" |
tail -1
Feb 26 21:49:23 smtp1 postfix/error[32347]: 4F0A73A11CF: to=<[email protected]>,
relay=none, delay=88661, delays=88661/0.02/0/0.05, dsn=4.4.1, status=deferred (delivery
temporarily suspended: connect to xxxxxxxxxxxxxxxxxxxx[x.x.x.x]:25: Connection timed out)
real 0m2.979s
user 0m0.280s
sys 0m0.680s
しかし、TACとawkコマンド、パフォーマンスを兼ね備えしようとしたとき予想1ではありません。
time tac MAIL-POSTFIX-LOG-20160226.log | awk ' $5~"postfix/error" && $6~"4F0A73A11CF" ' |
head -1
Feb 26 21:49:23 smtp1 postfix/error[32347]: 4F0A73A11CF: to=<[email protected]>,
relay=none, delay=88661, delays=88661/0.02/0/0.05, dsn=4.4.1, status=deferred (delivery
temporarily suspended: connect to xxxxxxxxxxxxxxxxxxxx[x.x.x.x]:25: Connection timed out)
real 0m19.232s
user 0m2.840s
sys 0m4.836s
任意の提案
よろしく
を 'と'フィールド区切り記号として、あなたのawk状態で '〜'の代わりに '='を使うこともできます(フィールド番号も同様に変わることに注意してください)。 – jas
私は "["についてのコメントを理解していますか、私は試しました: time tac MAIL-POSTFIX-LOG-20160226.log | awk -F ':' '$ 3〜 "postfix/error" && $ 4 == "4F0A73A11CF"' |ヘッドは-1 本当0m16.003s ユーザーが SYS 0m3.984s は、私は4番目のフィールド「4F0A73A11CF」、大した改善のためのスペースを追加する必要がありました0m2.756s。 – Fdv
"grep -m1"のような最初のマッチの後にawkコマンドを停止することは可能です そうでなければtacを使用することは役に立たない – Fdv