以下是我的输入文件:
这些数据是分分钟的。
我需要将数据转化为分钟,通过对每一分钟的加和,即1-5,6-10等等。我需要这些数据在jfree图表中绘制。请建议如何获得输出
输入:
11.01:5
11.02:4
11.03:3
11.04:8
11.05:2
12.11:3
12.12:4
12.13:1
12.15:0
13.03:04
22.56:01
22.57:03
22.58:2
23.00:0输出:
11.05:22
12.15:8
13.05:4
23.00:6发布于 2014-11-17 10:45:19
这个awk应该能工作:
awk -F '[.:,]' -v OFS=: '{
p=5*int(($2+4)/5);
$1=1*$1;
if(p==60){
p="0";
$1++
}
k=sprintf("%02d.%02d", $1, p)
}
!s[k]{
b[++n]=k
}
{
s[k]+=$3
}
END{
for (i=1; i<=n; i++)
print b[i],s[b[i]]
}' file
11.05:22
12.15:8
13.05:4
23.00:6发布于 2014-11-17 10:45:47
这是草稿。一旦您展示了您的一些尝试,我将提供一个更通用的方法:
awk -F"[.:]" -v OFS=":" '{r=sprintf("%d", ($2-1)/5); r=(r+1)*5; a[$1"."r]+=$3} END {for (i in a) print i, a[i]}' file对于给定的输入,它返回:
22.60:6
13.5:4
12.15:8
23.5:0
11.5:22关键是每1,2,3,4和5到5。我这样做是说:
d -> d-1 -> (d-1)/5 (int division) -> (d-1)/5 * 5为了确保不出现分钟60,您可以添加一些条件:if (r==60) {r=0; $1++}
$ awk -F"[.:]" -v OFS=":" '{r=sprintf("%d", ($2-1)/5); r=(r+1)*5; if (r==60) {r=0; $1++}; a[$1"."r]+=$3} END {for (i in a) print i, a[i]}' file
23.0:6
13.5:4
12.15:8
23.5:0
11.5:22您还可以做一些花哨的事情,比如打印一个领先的0分钟<10分钟,然后打印那些求和>0的值:
$ awk -F"[.:]" -v OFS=":" '{r=sprintf("%d", ($2-1)/5); r=(r+1)*5; if (r==60) {r=0; $1++}; r=sprintf("%02d", r); a[$1"."r]+=$3} END {for (i in a) if (a[i]) print i, a[i]}' file
11.05:22
12.15:8
13.05:4
23.00:6发布于 2014-11-17 10:44:03
这个问题可以用不同的方式解决: sed,awk,python等。
下面是如何使用常见的bash命令来完成这一任务。
#!/bin/bash
# extract the unique set of hours from the input (i.e. 11, 12, 13, 22, 23)
#
hours=$(cut -f 1 -d . test.txt | sort -u)
for hour in $hours; do
# initialize sum for this hour
#
count=0
# extract the number following the ':'
#
for x in $(grep "^$hour" test.txt | cut -f 2 -d :); do
# sum up the numbers following the ':'
#
count=$(($count + $x))
done
# Extract the last timestamp for the given hour
#
t=$(grep "^$hour" test.txt | tail -1 | cut -f 1 -d :)
# Print the desired output of timestamp:sum
#
echo $t:$count
done这是输出:
11.05:22
12.15:8
13.03:4
22.58:6
23.00:0您的输入存储在文件test.txt中:
11.01:5
11.02:4
11.03:3
11.04:8
11.05:2
12.11:3
12.12:4
12.13:1
12.15:0
13.03:04
22.56:01
22.57:03
22.58:2
23.00:0让我指出我们在脚本中看到的一些命令
cut用于从输入中提取列。cut -f 1 -d .将从您的输入中提取该小时。-f 1说打印列1,-d .说使用'.‘作为分隔符。tail -f 1从尾巴的输入中打印最后一行。sort -u从输入中删除重复行。https://stackoverflow.com/questions/26970556
复制相似问题