awk 在文件每行后面增加個時間戳


需求

通過執行命令得出一個結果,需要給結果加入時間戳。

/opt/thomsonreuters/ads/bin/adsmon -print | grep -B1 -A4 -E "position:.*snowballfinance.com" | grep -vE 'channel:|applicationKey:|protocol:' | sed 's/\/.*//g' | tr -d '[:blank:]' | sed 's/--/#:#/g' | awk -F ':' '{print $2}' | tr '\n' ' ' | tr '#' '\n'|awk -v trepIp=`ifconfig eth0|sed -n '2p'|cut -d":" -f2|cut -d" " -f1` '{print trepIp"_"$1"_"$2,$3}'|awk '{a[$1]+=$2}END{for (key in a) print key,a[key]}' |sort

輸出結果:

10.21.254.105_hk-broker-sh_10.21.0.6 8763
10.21.254.105_hk-pankou-sh_10.21.0.5 8763
10.21.254.105_hk-stock-sh_10.21.0.1 163
10.21.254.105_hk-stock-sh_10.21.0.7 3916
10.21.254.105_hk-stock-sh_10.21.0.8 2757
10.21.254.105_hk-stock-sh_10.21.0.9 2090
10.21.254.105_us-futures-sh_10.21.0.3 1
10.21.254.105_us-futures-sh_10.21.0.6 109
10.21.254.105_us-option-sh_10.21.0.1 175001
10.21.254.105_us-option-sh_10.21.0.2 118702
10.21.254.105_us-option-sh_10.21.0.3 135658
10.21.254.105_us-option-sh_10.21.0.4 171277
10.21.254.105_us-stock-sh_10.21.0.5 4572
10.21.254.105_us-stock-sh_10.21.0.7 8309
10.21.254.105_us-stock-sh_10.21.0.8 17
radmin@TREP2# 

在前面的命令后加管道 和 awk '{ print $0" " strftime("%Y-%m-%d %H:%M:%S",systime())}' 進行添加時間戳

完整命令如下:

/opt/thomsonreuters/ads/bin/adsmon -print | grep -B1 -A4 -E "position:.*snowballfinance.com" | grep -vE 'channel:|applicationKey:|protocol:' | sed 's/\/.*//g' | tr -d '[:blank:]' | sed 's/--/#:#/g' | awk -F ':' '{print $2}' | tr '\n' ' ' | tr '#' '\n'|awk -v trepIp=`ifconfig eth0|sed -n '2p'|cut -d":" -f2|cut -d" " -f1` '{print trepIp"_"$1"_"$2,$3}'|awk '{a[$1]+=$2}END{for (key in a) print key,a[key]}' |sort |awk '{ print $0" " strftime("%Y-%m-%d %H:%M:%S",systime())}'

輸出結果:

10.21.254.105_hk-stock-sh_10.21.0.1 0 2020-08-20 17:02:27
10.21.254.105_us-futures-sh_10.21.0.3 0 2020-08-20 17:02:27
10.21.254.105_us-futures-sh_10.21.0.6 109 2020-08-20 17:02:27
10.21.254.105_us-option-sh_10.21.0.1 175001 2020-08-20 17:02:27
10.21.254.105_us-option-sh_10.21.0.2 118702 2020-08-20 17:02:27
10.21.254.105_us-option-sh_10.21.0.3 135658 2020-08-20 17:02:27
10.21.254.105_us-option-sh_10.21.0.4 171277 2020-08-20 17:02:27
10.21.254.105_us-stock-sh_10.21.0.5 4572 2020-08-20 17:02:27
10.21.254.105_us-stock-sh_10.21.0.7 8309 2020-08-20 17:02:27
10.21.254.105_us-stock-sh_10.21.0.8 17 2020-08-20 17:02:27

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM