Murlidhar Fichadia Murlidhar Fichadia - 5 months ago 16
Bash Question

print values in custom format in AWK

How do I align min and max values shown below

Program



{
for(i = 1; i <= NF; i++)
{
a[i] = $i;
if (min[i]==""){ min[i]=$i;} #line1
if (max[i]==""){ max[i]=$i;} #line2
if ($i<min[i]) { min[i]=$i;} #line3
if ($i>max[i]) { max[i]=$i;} #line4
}
print $0;

}
END {
OFS="\n";
for(j = 1; j <= NF; j++)
{
print min[j],max[j];
}
}


Dataset



4 14 24 34
3 13 23 33
1 11 21 31
2 12 22 32
5 15 25 35


Current Output



4 14 24 34
3 13 23 33
1 11 21 31
2 12 22 32
5 15 25 35
1
5
11
15
21
25
31
35


Output I need



4 14 24 34
3 13 23 33
1 11 21 31
2 12 22 32
5 15 25 35
1 11 21 31 -> Min Values(for that field)
5 15 25 35 -> Max Values(for that field)

Answer

You need to loop twice, once for the min, once for the max:

END {
    for(j = 1; j <= NF; j++) printf "%d ", min[j]
    print ""
    for(j = 1; j <= NF; j++) printf "%d ", max[j]
    print ""
}