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

。_饼干妹妹 提交于 2020-01-25 06:51:11

问题


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.

My desire output is:

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

Where

7 is the number of counts until next 0 (i.e. 2,3,2,3,2,20,2) and 20 is the maximum value among them
1 is the number of counts until next 0 (i.e. 2) and 2 is the maxmimum
6 is the number of counts until next 0 (i.e. 2,1,2,5,6,7) and 7 is the maximum among them
and so on

Although I could succeed it by fortran, but I am looking for to do it in shell script.

I used following algorithm in my fortran:

  n=0
  for i in {1..24};do  #assigned all values to a[i]
  if a[i] > 0; then n=n+1; a1[i]=a[i]  #a1[i]= assign the values of a[i] until next 0
  elif a[i] = 0; then c1[i]=n; c2[i]=maximum[c1(i)]; n=0 #c1=count all a1[i]; c2=find their maximum

回答1:


Could you please try following.

awk '
$0!=0{
  count++
  max=max>$0?max:$0
}
$0==0{
  if(count){
      print count,max
  }
  count=max=""
}
END{
  if(count){
      print count,max
  }
}
'  Input_file

Output will be as follows.

7 20
1 2
6 7
1 3
3 5


Explanation: Adding explanation for above code.

awk '                           ##Starting awk program from here.
$0!=0{                          ##Checking condition if Line is NOT equal to zero then do following.
  count++                       ##Increasing variable count with value with 1 each time it comes here.
  max=max>$0?max:$0             ##Creating variable max whose value is always greater than lines.
}
$0==0{                          ##Checking condition if a line value is ZERO then do following.
  if(count){                    ##Checking condition if variable count is NOT NULL then do following.
      print count,max           ##Printing count and max variables here.
  }
  count=max=""                  ##Nullifying count and max variables here.
}
END{                            ##Starting END section of this awk program here.
  if(count){                    ##Checking condition if variable count is NOT NULL then do following.
      print count,max           ##Printing count and max variables here.
  }
}
'  Input_file                   ##Mentioning Input_file name here.



回答2:


Pure bash version

maxDuration() {
    local crtMax=0 lineCnt=0 crtVal;
    while read crtVal; do
        ((crtVal)) && ((lineCnt++,crtMax=crtVal>crtMax?crtVal:crtMax)) || { 
            ((lineCnt)) && echo $lineCnt $crtMax;
            crtMax=0 lineCnt=
        };
    done;
    ((lineCnt)) && echo $lineCnt $crtMax
}

Then

maxDuration <ifile.txt
7 20
1 2
6 7
1 3
3 5

more golfed

{ n=0 m=0;while read i;do ((i?(n++,m=i>m?i:m,0):n))&&{
echo $n $m;m=0 n=0;};done;((n))&&echo $n $m;} <ifile.txt


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

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