AWK - find min value of each row with arbitrary size

后端 未结 2 1691
借酒劲吻你
借酒劲吻你 2020-12-20 06:12

I have a file with the lines as:

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 abo

2条回答
  •  无人及你
    2020-12-20 07:14

    Your problem is pretty simple. All you need to do is to define a variable min in the BEGIN part of your script, and at each line, you just have to perform a simple C-like algorithm for minimum element (set the first field as min, and then perform a check with the next field, and so on until you reach the final field of the line). The total number of fields in the line will be known to you because of the variable NF. So its just a matter of writing a for loop. Once the for loop is fully executed for the line, you will have the minimum element with you, and you could just print it.

提交回复
热议问题