您好我有一个日志文件trace.log,它打印时间戳,线程名称和交易方法以及交易ID,如下所示。
2014-01-23 15:50:41,724 [catalina-exec-35] INFO TRANSACTION getConnection REQUEST, ID=1308::
2014-01-23 15:50:41,725 [catalina-exec-33] INFO TRANSACTION getConnection REQUEST, ID=1304::
2014-01-23 15:50:41,727 [catalina-exec-10] INFO TRANSACTION getConnection REQUEST, ID=1298::
2014-01-23 15:50:41,727 [catalina-exec-24] INFO TRANSACTION getConnection REQUEST, ID=1307::
2014-01-23 15:50:41,727 [catalina-exec-12] INFO TRANSACTION getConnection DONE, ID=1305::
2014-01-23 15:50:41,733 [catalina-exec-10] INFO TRANSACTION getConnection DONE, ID=1298::
2014-01-23 15:50:41,734 [catalina-exec-26] INFO TRANSACTION getConnection REQUEST, ID=1313::
2014-01-23 15:50:41,734 [catalina-exec-26] INFO TRANSACTION getConnection DONE, ID=1313::
2014-01-23 15:50:41,738 [catalina-exec-39] INFO TRANSACTION getConnection REQUEST, ID=1311::
2014-01-23 15:50:41,733 [catalina-exec-35] INFO TRANSACTION getConnection DONE, ID=1308::
2014-01-23 15:50:41,738 [catalina-exec-27] INFO TRANSACTION getConnection REQUEST, ID=1309::
2014-01-23 15:50:41,737 [catalina-exec-22] INFO TRANSACTION getConnection REQUEST, ID=1310::
2014-01-23 15:50:41,743 [catalina-exec-30] INFO TRANSACTION getConnection REQUEST, ID=1315::
2014-01-23 15:50:41,744 [catalina-exec-39] INFO TRANSACTION getConnection DONE, ID=1311::
2014-01-23 15:50:41,747 [catalina-exec-2] INFO TRANSACTION getConnection REQUEST, ID=1318::
我想grep并打印getConnection REQUEST和getConnection DONE的时间戳,将一行中的特定ID打印到文件中。
我有一个shell脚本,可以在多行中打印时间戳,如下所示。 这是我的shell脚本
for i in {1..800}
do
echo "Welcome $i times"
echo "ID=$i, getConnection " >> time.log
grep ID=$i: trace.log | grep getConnection | cut -d'[' -s -f1 >> time.log
echo " " >> time.log
done
输出如下所示
ID=791, getConnection
2014-01-23 15:50:16,703
2014-01-23 15:50:16,706
ID=792, getConnection
2014-01-23 15:50:16,704
2014-01-23 15:50:16,704
ID=793, getConnection
2014-01-23 15:50:16,704
2014-01-23 15:50:16,709
ID=794, getConnection
2014-01-23 15:50:16,708
2014-01-23 15:50:16,712
我怎么能这样做?请帮忙
我需要输出如下所示
ID=792, getConnection 2014-01-23 15:50:16,703 2014-01-23 15:50:16,706
ID=792, getConnection 2014-01-23 15:50:16,704 2014-01-23 15:50:16,704
ID=793, getConnection 2014-01-23 15:50:16,704 2014-01-23 15:50:16,709
ID=794, getConnection 2014-01-23 15:50:16,708 2014-01-23 15:50:16,712
答案 0 :(得分:1)
管道到tr
以删除换行符,如下所示:
grep ID=$i: trace.log | grep getConnection | cut -d'[' -s -f1 | tr -d '\n'
答案 1 :(得分:1)
使用awk
,您可以通过更改此命令来保存一些命令:
grep ID=$i: trace.log | grep getConnection | cut -d'[' -s -f1
为:
awk '$0~id && /getConnection/ {print $1,$2}' id=$i trace.log
2014-01-23 15:50:41,724
2014-01-23 15:50:41,733
为什么不让awk
做所有事情(使用您的数据):
awk -F" \\\[|=" '{split($3,i,":")} /getConnection REQUEST/ {r[i[1]]=$1} /getConnection DONE/ {d[i[1]]=$1} END {for (j in r) print "ID=" j " getConnection " r[j] "," d[j]}' file
ID=1318 getConnection 2014-01-23 15:50:41,747,
ID=1309 getConnection 2014-01-23 15:50:41,738,
ID=1298 getConnection 2014-01-23 15:50:41,727,2014-01-23 15:50:41,733
ID=1310 getConnection 2014-01-23 15:50:41,737,
ID=1311 getConnection 2014-01-23 15:50:41,738,2014-01-23 15:50:41,744
ID=1313 getConnection 2014-01-23 15:50:41,734,2014-01-23 15:50:41,734
ID=1304 getConnection 2014-01-23 15:50:41,725,
ID=1315 getConnection 2014-01-23 15:50:41,743,
ID=1307 getConnection 2014-01-23 15:50:41,727,
ID=1308 getConnection 2014-01-23 15:50:41,724,2014-01-23 15:50:41,733
某些更改版本并添加了sort
awk -F" \\\[|=" '{split($3,i,":")} g {/DONE/?d[i[1]]=$1:r[i[1]]=$1} END {for (j in r) print "ID="j,g,r[j]","d[j]}' g="getConnection" file | sort -k1
ID=1298 getConnection 2014-01-23 15:50:41,727,2014-01-23 15:50:41,733
ID=1304 getConnection 2014-01-23 15:50:41,725,
ID=1307 getConnection 2014-01-23 15:50:41,727,
ID=1308 getConnection 2014-01-23 15:50:41,724,2014-01-23 15:50:41,733
ID=1309 getConnection 2014-01-23 15:50:41,738,
ID=1310 getConnection 2014-01-23 15:50:41,737,
ID=1311 getConnection 2014-01-23 15:50:41,738,2014-01-23 15:50:41,744
ID=1313 getConnection 2014-01-23 15:50:41,734,2014-01-23 15:50:41,734
ID=1315 getConnection 2014-01-23 15:50:41,743,
ID=1318 getConnection 2014-01-23 15:50:41,747,