在Shell脚本中找到数据集之间的持续时间及其最大值 [英] Find the durations and their maximum between the dataset in shell script

查看:45
本文介绍了在Shell脚本中找到数据集之间的持续时间及其最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集为:

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

我想找出不同的持续时间,以及它们在0个值之间的最大值.

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

我的愿望输出是:

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

哪里

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

尽管我可以通过fortran使其成功,但是我正在寻找在shell脚本中实现的方法.

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

我在fortran中使用了以下算法:

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

推荐答案

能否请您尝试以下操作.

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

输出如下.

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.

这篇关于在Shell脚本中找到数据集之间的持续时间及其最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆