在shell脚本中以一个间隔查找持续时间及其在数据集之间的最大值 [英] Find the durations and their maximum between the dataset in an interval in shell script
问题描述
这与我的老问题有关我的数据集为:
ifile.txt
2
3
2
3
2
20
2
0
2
0
0
2
1
2
5
6
7
0
3
0
3
4
5
我想找出6个值间隔中的0个值之间的不同持续时间及其最大值.
I would like to find out different duration and their maximum between the 0 values in 6 values interval.
我的愿望输出是:
ofile.txt
6 20
1 2
1 2
1 2
5 7
1 3
3 5
哪里
6 is the number of counts until next 0 within 6 values (i.e. 2,3,2,3,2,20) and 20 is the maximum value among them;
1 is the number of counts until next 0 within next 6 values (i.e. 2,0,2,0,0,2) and 2 is the maxmimum;
Next 1 and 2 are withing same 6 values;
5 is the number of counts until next 0 within next 6 values (i.e. 1,2,5,6,7,0) and 7 is the maximum among them;
And so on
根据上一个问题的答案,我正在尝试以下方法:
As per the answer in my previous question, I was trying with this:
awk '(NR%6)==0
$0!=0{
count++
max=max>$0?max:$0
}
$0==0{
if(count){
print count,max
}
count=max=""
}
END{
if(count){
print count,max
}
}
' ifile.txt
推荐答案
一种格式命令已添加到RavinderSingh13提供的 EDIT2 解决方案中,该命令将打印准确的期望输出:
A format command added to the EDIT2 solution given by RavinderSingh13 which will print exact desire output:
awk '
$0!=0{
count++
max=max>$0?max:$0
found=""
}
$0==0{
print count,max
count=max=0
next
}
FNR%6==0{
print count,max
count=max=0
found=1
}
END{
if(!found){
print count,max
}
}
' Input_file | awk '!/^ /' | awk '$1 != 0'
输出如下.
6 20
1 2
1 2
1 2
5 7
1 3
3 5
:添加了另一个解决方案,该解决方案将每6个元素中的值以及其中的零打印出来.
Adding another solution which will print values in every 6 elements along with zeros coming in between.
awk '
$0!=0{
count++
max=max>$0?max:$0
found=""
}
$0==0{
print count,max
count=max=0
next
}
FNR%6==0{
print count,max
count=max=0
found=1
}
END{
if(!found){
print count,max
}
}
' Input_file
输出如下.
6 20
1 2
1 2
0 0
1 2
5 7
1 3
3 5
根据OP的注释,在这种情况下,当零值为零时,OP不想重置非零计数.
As per OP's comment OP doesn't want to reset of count of non-zeros when a zero value comes in that case try following.
awk '
$0!=0{
count++
max=max>$0?max:$0
found=""
}
FNR%6==0{
print count,max
count=max=0
found=1
}
END{
if(!found){
print count,max
}
}
' Input_file
输出如下.
6 20
3 2
5 7
.......
请您尝试以下操作(仅针对已发布的示例进行书面和测试).
Could you please try following(written and tested with posted samples only).
awk '
$0!=0{
count++
max=max>$0?max:$0
found=""
}
$0==0{
count=FNR%6==0?count:0
found=""
}
FNR%6==0{
print count,max
count=max=0
found=1
}
END{
if(!found){
print count,max
}
}
' Input_file
这篇关于在shell脚本中以一个间隔查找持续时间及其在数据集之间的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!