需要從json日誌文件讀取數據並從使用shell獲取分析數據腳本。如何讀取shell腳本.sh文件中的json日誌文件,並通過計算日誌文件中出現的次數來獲取分析數據
日誌文件包含如下JSON:
{
info: 'label1',
description: 'some desc',
timestamp: '2016-07-27T06:24:50.335Z'
}
{
info: 'label2',
description: 'some desc',
timestamp: '2016-07-27T06:24:50.335Z'
}
{
info: 'label2',
description: 'some desc',
timestamp: '2016-07-27T06:24:50.335Z'
}
{
info: 'label2',
description: 'some desc',
timestamp: '2016-07-29T06:24:50.335Z'
}
{
info: 'label3',
description: 'some desc',
timestamp: '2016-07-29T06:24:50.335Z'
}
我需要的結果如下(使用shell腳本):
Labels Date Count
label1 2016-07-27 1
label2 2016-07-27 2
label2 2016-07-29 1
label3 2016-07-29 1
這是到目前爲止,我可以去,需要一些關於如何處理的建議。\
#!/bin/bash
my_dir=`dirname $0`
file="out.log"
#keysFile="$my_dir/keys.txt"
for log in $(cat $file | jq '{id: .info,time: .timestamp}'); do
#This is as far as I could get. I was able to read the data in the form of {id: 'label1', time: '2016-07-27T06:24:50.335Z' }
#Now I need to somehow create a key value thing in shell and store timestamp/label as key and increment the count
echo $log
done
那麼...你試過了嗎?你玩過'jq'嗎? – fedorqui
一旦你發佈了一個問題,還提供你已經嘗試過的示例代碼或代碼片段。如果可能的話,你也可以指出代碼中拋出錯誤的部分。一旦您提供這些詳細信息,SO社區將能夠爲您提供幫助,而不僅僅是QA論壇。 – KartikKannapur
@KartikKannapur更新了帖子。 – Arun