列中的最小值、平均值和最大值

发布于 2025-01-03 05:02:58 字数 1385 浏览 1 评论 0原文

我有下一个问题,我想要有两个最小值、一个最大值和此输入的平均值:

 Call 1 1.160 ms
 Call 2 0.163 ms
 Call 3 1.154 ms
 Call 4 1.029 ms
 Call 5 0.291 ms
 Call 6 1.186 ms
 Call 7 1.089 ms
 Call 8 1.122 ms
 Call 9 0.975 ms
 Call 10 1.224 ms
 Call 11 0.965 ms
 Call 12 1.047 ms
 Call 13 1.138 ms
 Call 14 1.217 ms
 Call 15 1.189 ms
 Call 16 1.115 ms
 Call 17 0.950 ms
 Call 18 1.112 ms
 Call 19 1.227 ms
 Call 20 1.071 ms
 Call 21 1.108 ms
 Call 22 1.191 ms
 Call 23 1.139 ms
 Call 24 1.032 ms
 Call 25 1.305 ms
 Call 26 1.085 ms
 Call 27 1.404 ms
 Call 28 1.282 ms
 Call 29 1.031 ms
 Call 30 1.018 ms
 Call 31 1.308 ms
 Call 32 1.299 ms
 Call 33 1.096 ms
 Call 34 1.129 ms
 Call 35 1.271 ms
 Call 36 1.035 ms
 Call 37 1.238 ms
 Call 38 1.150 ms
 Call 39 1.042 ms
 Call 40 0.948 ms

我一直在使用这些命令:

 awk 'min=="" || $3 < min {min=$3; minline=$0}; END{ print minline}'

 awk '{ if ($3>max) {max=$3; line=$0} } END {print line }'

 awk '{s+=$3} END{print "Number of Calls: "NR, "\nAverage: "s/(NR)}'

输出是这样的:

 Minimun: Call 18 0.212 ms
 Maximun: Call 27 1.404 ms
 Number of Calls: 40
 Average: 1.1071

但我需要有 2 个最小值,其中之一它们如上所示,但另一个应该是大于 0.800 的任何数字。我尝试过这个:

 awk 'min=="0.800" || $3 < min {min=$3; minline=$0}; END{ print minline}'

但它没有向我显示任何内容。拜托,我需要你的帮助才能将其放入脚本中。

预先感谢您的帮助

I have the next question, I'll like to have TWO minimun values, a maximun value and the average for this input:

 Call 1 1.160 ms
 Call 2 0.163 ms
 Call 3 1.154 ms
 Call 4 1.029 ms
 Call 5 0.291 ms
 Call 6 1.186 ms
 Call 7 1.089 ms
 Call 8 1.122 ms
 Call 9 0.975 ms
 Call 10 1.224 ms
 Call 11 0.965 ms
 Call 12 1.047 ms
 Call 13 1.138 ms
 Call 14 1.217 ms
 Call 15 1.189 ms
 Call 16 1.115 ms
 Call 17 0.950 ms
 Call 18 1.112 ms
 Call 19 1.227 ms
 Call 20 1.071 ms
 Call 21 1.108 ms
 Call 22 1.191 ms
 Call 23 1.139 ms
 Call 24 1.032 ms
 Call 25 1.305 ms
 Call 26 1.085 ms
 Call 27 1.404 ms
 Call 28 1.282 ms
 Call 29 1.031 ms
 Call 30 1.018 ms
 Call 31 1.308 ms
 Call 32 1.299 ms
 Call 33 1.096 ms
 Call 34 1.129 ms
 Call 35 1.271 ms
 Call 36 1.035 ms
 Call 37 1.238 ms
 Call 38 1.150 ms
 Call 39 1.042 ms
 Call 40 0.948 ms

I've been using those commands:

 awk 'min=="" || $3 < min {min=$3; minline=$0}; END{ print minline}'

 awk '{ if ($3>max) {max=$3; line=$0} } END {print line }'

 awk '{s+=$3} END{print "Number of Calls: "NR, "\nAverage: "s/(NR)}'

The output is this:

 Minimun: Call 18 0.212 ms
 Maximun: Call 27 1.404 ms
 Number of Calls: 40
 Average: 1.1071

But I need to have 2 minimun values, one of them is showed above but the other one should be any number greather than 0.800. I've trying this:

 awk 'min=="0.800" || $3 < min {min=$3; minline=$0}; END{ print minline}'

But it shows me nothing. Please, I need your help to place this in a script.

Thank you in advance for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2025-01-10 05:02:58
kent$  awk 'BEGIN{min=999}
{a[NR]=$0;if($3<min){min=$3;m[1]=NR;}if($3>max){max=$3;m[2]=NR;}m[3]+=$3;}
END{print "Min:"a[m[1]];
        print "Max:"a[m[2]];
        print "Number Of Calls:" NR;
        print "Avg:"m[3]/NR;
        t=max>0.800?a[m[2]]:"None";
        print "Any greater than 0.800:"t}' yourFile

输出:

Min:Call 2 0.163 ms
Max:Call 27 1.404 ms
Number Of Calls:40
Avg:1.08837
Any greater than 0.800:Call 27 1.404 ms

注意:对于“任何大于 0.800 的数字”,我只是检查了 Max,如果它 > 0.800,则将其打印为幻数。如果没有,则打印 None。

编辑

对OP评论的更改:

kent$  awk 'BEGIN{min=mag=999}
{a[NR]=$0;if($3<min){min=$3;m[1]=NR;}if($3>max){max=$3;m[2]=NR;}if($3>0.800&&$3<mag){mag=$3;m[4]=NR} m[3]+=$3;}
END{print "Min:"a[m[1]];
        print "Max:"a[m[2]];
        print "Number Of Calls:" NR;
        print "Avg:"m[3]/NR;
        print "Any greater than 0.800:"a[m[4]]}' yourFile

输出:

Min:Call 2 0.163 ms
Max:Call 27 1.404 ms
Number Of Calls:40
Avg:1.08837
Any greater than 0.800:Call 40 0.948 ms
kent$  awk 'BEGIN{min=999}
{a[NR]=$0;if($3<min){min=$3;m[1]=NR;}if($3>max){max=$3;m[2]=NR;}m[3]+=$3;}
END{print "Min:"a[m[1]];
        print "Max:"a[m[2]];
        print "Number Of Calls:" NR;
        print "Avg:"m[3]/NR;
        t=max>0.800?a[m[2]]:"None";
        print "Any greater than 0.800:"t}' yourFile

output:

Min:Call 2 0.163 ms
Max:Call 27 1.404 ms
Number Of Calls:40
Avg:1.08837
Any greater than 0.800:Call 27 1.404 ms

Note: for the "any number greater than 0.800", i just checked the Max, if it >0.800,print it as the magic number. if not, print None.

EDIT

changes for OP's comment:

kent$  awk 'BEGIN{min=mag=999}
{a[NR]=$0;if($3<min){min=$3;m[1]=NR;}if($3>max){max=$3;m[2]=NR;}if($3>0.800&&$3<mag){mag=$3;m[4]=NR} m[3]+=$3;}
END{print "Min:"a[m[1]];
        print "Max:"a[m[2]];
        print "Number Of Calls:" NR;
        print "Avg:"m[3]/NR;
        print "Any greater than 0.800:"a[m[4]]}' yourFile

output:

Min:Call 2 0.163 ms
Max:Call 27 1.404 ms
Number Of Calls:40
Avg:1.08837
Any greater than 0.800:Call 40 0.948 ms
街角卖回忆 2025-01-10 05:02:58

您想要两个最小值:

awk '{ if (min1==""||$3<=min1) { min2=min1; line2=line1; min1=$3; line1=$0; }
       else if (min2=="" || $3 <= min2)                { min2=$3; line2=$0; } }
       END{ print line1; print line2}'

输出:

Call 2 0.163 ms
Call 5 0.291 ms

您想要最小值和第二个最小值大于 0.8 毫秒:

awk '{ if (min1==""||$3<=min1) { if(min1>0.8) {min2=min1; line2=line1;} min1=$3; line1=$0; }
       else if ($3>0.8 && (min2=="" || $3 <= min2))                   { min2=$3; line2=$0; } }
       END{ print line1; print line2}'

输出:

 Call 2 0.163 ms
 Call 40 0.948 ms

You want the two minimal values:

awk '{ if (min1==""||$3<=min1) { min2=min1; line2=line1; min1=$3; line1=$0; }
       else if (min2=="" || $3 <= min2)                { min2=$3; line2=$0; } }
       END{ print line1; print line2}'

output:

Call 2 0.163 ms
Call 5 0.291 ms

You want the minimal value and the second minimal value grater than 0.8 ms:

awk '{ if (min1==""||$3<=min1) { if(min1>0.8) {min2=min1; line2=line1;} min1=$3; line1=$0; }
       else if ($3>0.8 && (min2=="" || $3 <= min2))                   { min2=$3; line2=$0; } }
       END{ print line1; print line2}'

output:

 Call 2 0.163 ms
 Call 40 0.948 ms
戴着白色围巾的女孩 2025-01-10 05:02:58

解决方案 1

这是我在 Perl 中的解决方案,我是一个新手,但想学习:

#!/usr/bin/perl -n

BEGIN { $min1 = 9999; $min2 = 9999; $max = -1; }

($label, $callNumber, $callTime, $ms) = split;

$sum += $callTime;
if ($max < $callTime) {
    $max = $callTime;
    $maxLine = $_; 
}   

if ($min1 > $callTime) {
    $min1 = $callTime;
    $min1Line = $_; 
}   

if ($min2 > $callTime && $min1 != $callTime) {
    $min2 = $callTime;
    $min2Line = $_; 
}   

END {
    $average = $sum / $.; 
    print "Minimum1:        $min1Line";
    print "Minimum2:        $min2Line";
    print "Maximum:         $maxLine";
    print "Number of Calls: $.\n";
    print "Average:         $average\n";
}   

唯一棘手的部分是计算 $min2,它是第二小的值。我的脚本看起来很长,但我相信它比其他一些解决方案更容易理解和维护。

方案2

该方案只计算两个min,一个max,但是比较短:

sort -k 3,3 -n data.txt | sed -n '1,2s/^/Min: /p;$s/^/Max: /p'

Solution 1

Here is my solution in Perl, in which I am a newbie, but would like to learn:

#!/usr/bin/perl -n

BEGIN { $min1 = 9999; $min2 = 9999; $max = -1; }

($label, $callNumber, $callTime, $ms) = split;

$sum += $callTime;
if ($max < $callTime) {
    $max = $callTime;
    $maxLine = $_; 
}   

if ($min1 > $callTime) {
    $min1 = $callTime;
    $min1Line = $_; 
}   

if ($min2 > $callTime && $min1 != $callTime) {
    $min2 = $callTime;
    $min2Line = $_; 
}   

END {
    $average = $sum / $.; 
    print "Minimum1:        $min1Line";
    print "Minimum2:        $min2Line";
    print "Maximum:         $maxLine";
    print "Number of Calls: $.\n";
    print "Average:         $average\n";
}   

The only tricky part is to calculate $min2, which is the next-to-smallest value. My script seems long, but I believe it is easier to understand and maintain than some other solutions.

Solution 2

This solution only calculates the two min, one max, but it is relatively short:

sort -k 3,3 -n data.txt | sed -n '1,2s/^/Min: /p;$s/^/Max: /p'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文