2016-06-11 6 views
2

をカウントするログファイルを読む様々なフィールドを抽出し、私はこのようなロガーファイルを持って出現

2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.commerce.common.utils.APIUtils - Enrichment data updated successful for partnumber : 13794017 with status : 201 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.commerce.common.utils.APIUtils - Enrichment data updated successful for partnumber : 13794017 with status : 201 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.commerce.common.utils.APIUtils - Enrichment data updated successful for partnumber : 13794017 with status : 201 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() 13794017 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken=1.808 sec 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() 13794017 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken=1.808 sec 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() 13794017 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken=1.808 sec 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() Scene7 update for 13794017 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken= 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() Scene7 update for 13794017 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken= 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() Scene7 update for 13794017 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken= 
2016-06-11 07:34:01.543 98460 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute EXIT 
2016-06-11 07:34:01.543 98460 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute EXIT 
2016-06-11 07:34:01.543 98460 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute EXIT 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.commerce.common.utils.APIUtils - Enrichment data updated successful for partnumber : 17696532 with status : 500 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.commerce.common.utils.APIUtils - Enrichment data updated successful for partnumber : 17696532 with status : 500 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.commerce.common.utils.APIUtils - Enrichment data updated successful for partnumber : 17696532 with status : 500 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() 17696532 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken=1.808 sec 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() 17696532 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken=1.808 sec 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() 17696532 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken=1.808 sec 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() Scene7 update for 17696532 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken= 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() Scene7 update for 17696532 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken= 
2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute() Scene7 update for 17696532 itemStatus itemsProcessed=1, itemsUpdated=1, timeTaken= 
2016-06-11 07:34:01.543 98460 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute EXIT 
2016-06-11 07:34:01.543 98460 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute EXIT 
2016-06-11 07:34:01.543 98460 [Thread-23-Job-parser-bolt] INFO JobLoader c.t.c.w.b.JobParserBolt - execute EXIT 

異なるpartnumbersとステータスコードと、そのような丸太の何百もの繰り返しがあります。 201以外のステータスコードを持つ別個のパーツ番号を別々のファイルに保存して、簡単に監視できるようにします。私は201の成功票すべてを数えたいと思っています。だから、私がこれから欲しいサンプル出力は次のようになります:

No. of partnumbers with Status 201: 1 
Partnumbers with Status 500: 17696532, ... , ... 
Partnumbers with Status 401: ... ,... 

私は最初にawkを使用しましたが、構文解析はそれほど簡単ではありません。また、同じパートナンバーが複数回表示されることに注意してください。どのようにして1つのパートナンバーを複数回カウントしないようにチェックを追加できますか?今まで

私のコードは:

awk -F'Enrichment data updated successful for partnumber :' '{print $2}' file.log |rev | cut -c 4- | rev 

は、私が最初にこのような部品番号を抽出したかったが、私は、複数の部品番号の問題を回避するには、チェックを適用することはできませんよ、それはそれでステータスコードを対応だ関連付けます。

答えて

2

これはawkを使用して解決された問題です。説明については、インラインコメントを参照してください。

awk '/Enrichment data updated successful for partnumber/ { 
    # store the results as a multidimensional array with the first key 
    # being the status and the key of the second array being the product 
    # number. This removes duplicates because array keys must be unique 
    arr[$NF][$16]++ 
} 
END { 
    # iterate over the 201 status items and count them 
    for (item in arr[201]) { 
     count++ 
    } 
    print "No. of partnumbers with Status 201: " count 

    # iterate over the status array 
    for (status in arr) { 
     # skip 201 status 
     if (status == 201) 
      continue 
     # join the array by "," for printing 
     # taken from http://stackoverflow.com/a/13648609/1032785 
     joined = sep = "" 
     for (product in arr[status]) { 
      joined = joined sep product 
      sep = "," 
     } 

     print "Partnumbers with Status " status ": " joined 
    } 
} 
' foo.log 

これは、私はいくつかの追加の行を追加して、あなたのサンプルログファイルで、次のような出力生成:

No. of partnumbers with Status 201: 1 
Partnumbers with Status 401: 17623039 
Partnumbers with Status 500: 17696532, 17696539 
+1

真の多次元配列にはGNU awkが必要です。 –

+0

@jordanmただ、知りたいのですが、NFは何[NF]ですか? –

+0

@jordanm、もしあれば 2016-06-11 07:34:01.542 98459 [Thread-23-Job-parser-bolt] INFO JobLoader ctcommerce.common.utils.APIUtils - パートナンバーの成功した更新データ:17696532 503 Service Unavailable、NFを使用することができますか、または20ドルを使用する必要がありますか? –

0

datamashpeeを使用してawkの、:

echo -n "No. of partnumbers with Status 201: " ; \ 
grep "status : " file.log | pee \ 
    'grep ": 201" | datamash -W -s countunique 16' \ 
    'grep -v ": 201" | datamash -W -s -g20 unique 16 | \ 
     sed "s/^[0-9]*/Partnumbers with Status &:/;s/,/, /g"' 

出力(OPのサンプルデータを使用して):

No. of partnumbers with Status 201: 1 
Partnumbers with Status 500: 17696532 
関連する問題