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

后端 未结 1 708
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 15:17

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         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-28 15:50

    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
    

    0 讨论(0)
提交回复
热议问题