0

我有一个包含两列的 csv 日志文件,每列都有请求(第一列)和响应(第二列)的时间戳。

以下是一些示例数据:

2013-07-11 08:39:08.748,2013-07-11 08:39:08.748
2013-07-11 08:39:08.826,2013-07-11 08:39:08.826
2013-07-11 08:39:08.860,2013-07-11 08:39:08.860
2013-07-11 08:39:08.919,2013-07-11 08:39:08.919
2013-07-11 08:39:08.941,2013-07-11 08:39:08.941
2013-07-11 08:39:09.390,2013-07-11 08:39:09.390
2013-07-11 08:39:09.594,2013-07-11 08:39:09.594
2013-07-11 08:39:09.619,2013-07-11 08:39:09.619
2013-07-11 08:39:09.787,2013-07-11 08:39:09.787
2013-07-11 08:39:10.006,2013-07-11 08:39:10.006
2013-07-11 08:39:10.017,2013-07-11 08:39:10.017
2013-07-11 08:39:10.088,2013-07-11 08:39:10.088
2013-07-11 08:39:10.214,2013-07-11 08:39:10.214

我想计算完整文件的两列(响应 - 请求)差异的平均值。该文件一天可以包含数百万个条目。

我正在寻找一种使用 shell 脚本的方法。请帮忙。

谢谢 Fedorqui,我试过你的脚本:

while read line; 
do 
d1=$(echo $line | cut -d, -f1); 
d2=$(echo $line | cut -d, -f2);  
ds1=$(date -d"$d1" "+%s"); 
ds2=$(date -d"$d2" "+%s"); 
echo "$ds2 - $ds1 = $(( $ds1 - $ds2))"; 
done < requestResponse.csv

它给了我以下结果:

1373543260 - 1373543260 = 0
1373543260 - 1373543260 = 0
1373543260 - 1373543260 = 0
1373543260 - 1373543260 = 0
1373543260 - 1373543260 = 0
1373543260 - 1373543260 = 0
1373543260 - 1373543260 = 0
1373543261 - 1373543261 = 0
1373543262 - 1373543262 = 0
1373543262 - 1373543262 = 0

我需要以毫秒为单位的差异。

4

1 回答 1

1

我在基本的 shell 脚本中用纳秒完成了它:

while read line
do
   d1=$(echo $line | cut -d, -f1)
   d2=$(echo $line | cut -d, -f2)
   ds1=$(date -d"$d1" "+%s.%N")
   ds2=$(date -d"$d2" "+%s.%N")
   diff=$(echo "$ds2 - $ds1" | bc)
   printf "$ds2 - $ds1 = %.3f\n" $diff
done < file

测试

$ while read line; do d1=$(echo $line | cut -d, -f1); d2=$(echo $line | cut -d, -f2); ds1=$(date -d"$d1" "+%s.%N"); ds2=$(date -d"$d2" "+%s.%N"); diff=$(echo "$ds2 - $ds1" | bc); printf "$ds2 - $ds1 = %.3f\n" $diff; done < file
1373524748.748000000 - 1373524748.748000000 = 0.000
1373524748.826000000 - 1373524748.826000000 = 0.000
1373524748.860000000 - 1373524748.860000000 = 0.000
1373524748.919000000 - 1373524748.919000000 = 0.000
1373524748.941000000 - 1373524748.941000000 = 0.000
1373524749.390000000 - 1373524749.390000000 = 0.000
1373524749.594000000 - 1373524749.594000000 = 0.000
1373524749.619000000 - 1373524749.619000000 = 0.000
1373524749.787000000 - 1373524749.787000000 = 0.000
1373524750.006000000 - 1373524750.006000000 = 0.000
1373524750.017000000 - 1373524750.017000000 = 0.000
1373524750.088000000 - 1373524750.088000000 = 0.000
1373524750.214000000 - 1373524750.214000000 = 0.000
于 2013-07-11T13:32:47.360 回答