Find the durations and their maximum between the dataset in an interval in shell script

血红的双手。 提交于 2019-12-13 03:46:21

问题


This is related to my older question Find the durations and their maximum between the dataset in shell script

I have a dataset as:

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

I would like to find out different duration and their maximum between the 0 values in 6 values interval.

My desire output is:

ofile.txt
6 20
1 2
1 2
1 2
5 7
1 3
3 5

Where

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

回答1:


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'

Output will be as follows.

6 20
1 2
1 2
1 2
5 7
1 3
3 5


EDIT2: 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

Output will be as follows.

6 20
1 2
1 2
0 0
1 2
5 7
1 3
3 5


EDIT: 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

Output will be as follows.

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


来源:https://stackoverflow.com/questions/58899361/find-the-durations-and-their-maximum-between-the-dataset-in-an-interval-in-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!