比较四列并打印出awk中编号最小的列

时间:2014-06-19 07:19:17

标签: bash shell awk

我有一个文件inp.txt比较列$5中的四列和打印最小列值。

 10; 10;     6;   4;
 11; 11;     7;   5;
 12; 12;     8;   6;
 13; 13;     9;   7;
 14; 14;    10;   8;
 15; 15;     1;   1;
 1;   3;     8;   8;
 2;   4;     9;   9;
 3;   5;    10;  10;
 4;   6;    11;  11;
 5;   7;    12;  12;

输出看起来像:

 10; 10;     6;   4; 4
 11; 11;     7;   5; 5
 12; 12;     8;   6; 6
 13; 13;     9;   7; 7
 14; 14;    10;   8; 8
 15; 15;     1;   1; 1
 1;   3;     8;   8; 1
 2;   4;     9;   9; 2
 3;   5;    10;  10; 3
 4;   6;    11;  11; 4
 5;   7;    12;  12; 5

1 个答案:

答案 0 :(得分:1)

使用awk你可以这样做:

awk -F'; *' '{min=$1; for (i=2; i<NF; i++) if ($i<min) min=$i; print $0 "\t" min}' file
10; 10;     6;   4; 4
11; 11;     7;   5; 5
12; 12;     8;   6; 6
13; 13;     9;   7; 7
14; 14;    10;   8; 8
15; 15;     1;   1; 1
1;   3;     8;   8; 1
2;   4;     9;   9; 2
3;   5;    10;  10; 3
4;   6;    11;  11; 4
5;   7;    12;  12; 5