AWK出力ヘルプ

AWK出力ヘルプ

現在、いくつかのApacheログを解析するために何かを書いていますが、systemコマンドが印刷の上に置かれているようです。まだこれをあまりやっていないawkので、おそらく非常に簡単な作業でしょう。

IFS=$'\n' 
for ja in `cat test.apache.access_log | awk '{print $1}' | sort -n | uniq -c | sort -rn | head -3`
do 
echo $ja|awk '{print "Count\tIP\t\tNSLookup"}{print $1"\t",$2,"\t\t",system("nslookup " $2"|grep name")}'
done

私が得るもの:

Count   IP              NSLookup
RR.ZZ.YY.XX.in-addr.arpa      name = ja.server.net.
241      XX.YY.ZZ.RR           0 

私が見たいことは:

Count   IP              NSLookup
241      XX.YY.ZZ.RR          RR.ZZ.YY.XX.in-addr.arpa      name = ja.server.net. 

ベストアンサー1

スクリプトを少し並べ替える必要があります。アッまったく必要ありません

echo -e 'Count\tIP\tNSLookup'
while read count line ; do
    echo -ne "$count\t$line\t"
    nslookup $line | grep name
done < <(cut -d' ' -f1 test.apache.access_log | sort | uniq -c | sort -rn | head -3)

もちろん、awkを介してのみ可能です。

awk '
    BEGIN{
        OFS="\t"
        print "Count", "IP", "NSLookup"
    }
    {
        A[$1]++
    }
    END{
        for(a in A){
            i = 3
            while(i > 0 && A[a] > A[B[i]]){
                B[i+1] = B[i]
                i--
            }
            B[i+1] = a
        }
        for(b=1; b<4; b++){
            "nslookup "B[b]" | grep name" | getline ns
            print A[B[b]], B[b], ns
        }
    }
    ' test.apache.access_log

おすすめ記事