ログから抽出

ログから抽出

実際のログは次のとおりです。

2016-06-19 22:08:09 [213917] 1bEgCe-000tZR-E9 ** [email protected] ([email protected]) <[email protected]> F=<[email protected]> P=<[email protected]> R=lookuphost T=remote_smtp H=mailin-01.mx.aol.com [64.12.88.131]:25 I=[36.23.21.11]:60147: SMTP error from remote mail server after initial connection: 554- (RTR:BL)  https://postmaster.aol.com/error-codes#554rtrbl\n554  Connecting IP: 36.23.21.11
2016-06-20 01:03:22 [516458] 1bEiwD-001zt7-IY ** [email protected] ([email protected]) <[email protected]> F=<[email protected]> P=<[email protected]> R=lookuphost T=remote_smtp H=mailin-02.mx.aol.com [64.12.88.163]:25 I=[36.23.21.14]:47630: SMTP error from remote mail server after initial connection: 554- (RTR:BL)  https://postmaster.aol.com/error-codes#554rtrbl\n554  Connecting IP: 36.23.21.14
2016-06-20 09:29:46 [256975] 1bEqpT-0014jI-HV ** [email protected] F=<[email protected]> P=<[email protected]> R=dkim_lookuphost T=dkim_remote_smtp H=mailin-04.mx.aol.com [64.12.88.132]:25 I=[36.23.21.11]:43705: SMTP error from remote mail server after initial connection: 421 DYN:T2  https://postmaster.aol.com/error-codes#554rtrbl\n554  Connecting IP: 36.23.21.11
2016-06-20 11:41:34 [413114] 1bEstm-001jSC-Ic ** [email protected] F=<[email protected]> P=<[email protected]> R=dkim_lookuphost T=dkim_remote_smtp H=mailin-02.mx.aol.com [64.12.91.195]:25 I=[36.23.21.14]:48714: SMTP error from remote mail server after initial connection: 421 DYN:T1  https://postmaster.aol.com/error-codes#554rtrbl\n554  Connecting IP: 36.23.21.14

私が欲しいもの:

Timestamp       EmailTo:        EmailFrom:               IPAddress:      ErrorCodes:
2016-06-19      [email protected]  [email protected]     36.23.21.11     554- (RTR:BL)
2016-06-20      [email protected]  [email protected]     36.23.21.14     554- (RTR:BL)
2016-06-20      [email protected]    [email protected]        36.23.21.11     421 DYN:T2
2016-06-20      [email protected] [email protected]  36.23.21.14     421 DYN:T1

次のコマンドから最初の3つのフィールドを抽出しました。

 echo -e "Timestamp\t\tEmailTo:\t\tEmailFrom:\t\t\t\t\t\t\t\tIPAddress:\tErrorCodes:" && awk 'NF>6 { d=6 ; while ( ! ($d ~ /^F=/ ) ) d++ ; printf "%s\t%s\t%s\n",$1,$6,substr($d,4,length($d)-4) ;} ' logs | column -t

みんなに感謝しますが、私はこれをしました:

 echo -e "Timestamp:\tEmailTo:\tEmailFrom:\t\tIPAddress:\tErrorCodes:" && awk 'NF>6 { d=6 ; while ( ! ($d ~ /^F=/ ) ) d++ ; print "%s\t%s\t%s\t%s\t%s\t%s\n",$1,$6,substr($d,4,length($d)-4),$NF,$(NF-5)$(NF-4) ; }' oops | column -t| grep -v "%s"

ベストアンサー1

あなたはawkを使って正しい道を進んでいます。タブ区切りフィールドを使用してログを読み書きするスクリプトを作成する必要があります。次に、列コマンドを使用して列を並べ替えます。

.awk²抽出:

BEGIN {OFS="\t"; print "Timestamp\tEmailTo:\tEmailFrom:\tIPAddress:\tErrorCodes:"}
{print $1, $6, $7, $NF, $(NF-5)}

次に、次のように実行します。

awk -f extract.awk logs | column -t -s '^I'

ここでは'^I'、引用符で囲まれた実際のタブを表します。

唯一のトリッキーな部分はログのエラーメッセージを処理することです。 IPとエラーコードフィールドの右側にある列を計算することでこの問題を解決しました。

出力は次のとおりです。

Timestamp   EmailTo:         EmailFrom:                      IPAddress:   ErrorCodes:
2016-06-19  [email protected]   ([email protected])     36.23.21.11  554-
2016-06-20  [email protected]   ([email protected])  36.23.21.14  554-
2016-06-20  [email protected]     F=<[email protected]>           36.23.21.11  421
2016-06-20  [email protected]  F=<[email protected]>     36.23.21.14  421

入力列に対する私の推測は、どちらがどちらであるかを指定しなかったので、おそらく間違っているでしょう。 3番目の列のEメールアドレスを整理したい場合は、おそらくawkに深すぎるでしょう。これで、PythonまたはPerlの使用を検討する必要がある時です。


または、データがない限り、目的の出力区切り記号を使用してください。その後、これは-sパラメータとして使用されますcolumn

²@Kusalanandaが指摘したように、awkスクリプトを1行のコードで書く理由はありません。これは彼のバージョンです:

BEGIN   {
    OFS="\t";
    print "Timestamp\tEmailTo:\tEmailFrom:\tIPAddress:\tErrorCodes:";
}

{
    print $1, $6, $7, $NF, $(NF-5);
}

個人的に好きなラインです。

おすすめ記事