EDIT2: Considering OP's edited samples could you please try following.
(EDIT2:考虑到OP的编辑示例,您可以尝试以下操作。)
I have tested it successfully with OP's 1st and latest edit samples and it worked perfectly fine with both of them. (我已经使用OP的第一个和最新的编辑示例成功地对其进行了测试,并且它们在两个示例中的运行都非常完美。)
Also one more thing, I made this solution such that a "corner case" where range could leave printing elements in case it is NOT crossing range value at last lines.
(还有另外一件事,我提出了这样的解决方案,以使“拐角情况”在不超过最后一行的范围值的情况下,范围可能会离开打印元素。)
Like OP's 1st sample where range=2
but max value is 5
so it will NOT leave 5 in here. (类似于OP的第一个样本,其中range=2
但最大值为5
因此此处不会留下5。)
sort -n Input_file |
awk -v range="2" '
!b[$1]++{
c[++count]=$1
}
{
d[$1]=(d[$1]?d[$1] OFS:"")$2
tot_element++
till=$1
}
END{
for(i=1;i<=till;i++){
num+=split(d[i],array," ")
if(++j==range){
start=start?start:1
printf("%s-%s %.02f
",start,i,num/tot_element)
start=i+1
j=num=""
delete array
}
if(j!="" && i==till){
printf("%s-%s %.02f
",start,i,num/tot_element)
}
}
}
'
Output will be as follows.
(输出如下。)
1-10 0.25
11-20 0.17
21-30 0.25
31-40 0.17
41-50 0.17
EDIT: In case your Input_file don't have 2nd column then try following.
(编辑:如果您的Input_file没有第二列,然后尝试以下操作。)
sort -k1 Input_file |
awk -v range="1" '
!b[$1]++{
c[++count]=$1
}
{
d[$1]=(d[$1]?d[$1] OFS:"")$0
tot_element++
till=$1
}
END{
for(i=1;i<=till;i+=(range+1)){
for(j=i;j<=i+range;j++){
num=split(d[c[j]],array," ")
total+=num
}
print i"-"i+range,tot_element?total/tot_element:0
total=num=""
}
}
'
Could you please try following, written and tested with shown samples.
(您能否请尝试按照显示的示例进行后续操作,编写并进行测试。)
sort -k1 Input_file |
awk -v range="1" '
!b[$1]++{
c[++count]=$1
}
{
d[$1]=(d[$1]?d[$1] OFS:"")$2
tot_element++
till=$1
}
END{
for(i=1;i<=till;i+=(range+1)){
for(j=i;j<=i+range;j++){
num=split(d[c[j]],array," ")
total+=num
}
print i"-"i+range,tot_element?total/tot_element:0
total=num=""
}
}
'
In case you don't have to include any 0
value then try following.
(如果您不必包含任何0
值,请尝试执行以下操作。)
sort -k1 Input_file |
awk -v range="1" '
!b[$1]++{
c[++count]=$1
}
{
d[$1]=(d[$1]!=0?d[$1] OFS:"")$2
tot_element++
till=$1
}
END{
for(i=1;i<=till;i+=(range+1)){
for(j=i;j<=i+range;j++){
num=split(d[c[j]],array," ")
total+=num
}
print i"-"i+range,tot_element?total/tot_element:0
total=num=""
}
}
'