AWK - 发现每行的最小值与任意大小 [英] AWK - find min value of each row with arbitrary size

查看:1293
本文介绍了AWK - 发现每行的最小值与任意大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与行的文件为:

5 3 6 4 2 3 5
1 4 3 2 6 5 8
..

我想获得每行的分,因此,例如与上面给出的投入,我应该得到:

I want to get the min on each line, so for example with the input given above, I should get:

min of first line:  2
min of second line: 1
..

我如何使用awk来为列的每一行任意数量做到这一点?

How can I use awk to do this for any arbitrary number of columns in each line?

推荐答案

如果你不使用数字代替文字介意输出您可以使用此单行:

If you don't mind the output using digits instead of words you can use this one liner:

$ awk '{m=$1;for(i=1;i<=NF;i++)if($i<m)m=$i;print "min of line",NR": ",m}' file
min of line 1:  2
min of line 2:  1

如果你确实想在序数数:

If you really do want to count in ordinal numbers:

BEGIN {
    split("first second third fourth",count," ")
}
{
    min=$1
    for(i=1;i<=NF;i++)
    if($i<min)
        min=$i

    print "min of",count[NR],"line: \t",min
}

保存这 script.awk 并运行,如:

$ awk -f script.awk file
min of first line:    2
min of second line:   1

显然,这将只适用于高达4行的文件工作,但只是增加了序数列表,你认为你需要的最大数量。你应该能够很容易地找到一个列表的在线pretty。

Obviously this will only work for files with upto 4 lines but just increase the ordinal numbers list to the maximum number you think you will need. You should be able to find a list online pretty easily.

这篇关于AWK - 发现每行的最小值与任意大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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