Hello Experts,
I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example:
cat file.txt
a 1 4
b 2 5
c 3 6
I want ouput like:
cat file.txt
a 1 4
b 2 5
c 3 6
Max 3 6
Min 1 4
Diff 2 2
awk 'min=="" || $2 < min {min=$2} END{print min}' file.txt
this awk command can find min from 2 column, but i have to change the column number, is there a one liner which can give me desired output in the same text file.
Thanks in advance.
Loop around the number of fields and create 2 arrays.
e.g.
awk '
{
for (i=2;i<=NF;i++) {
if (i in min) {
if ($i < min) {
min=$i;
}
} else {
min=$i;
}
if (i in max) {
...etc...
}
}1
END {
printf ("Min: ");
for (i in min) {
printf ("%d ", min)
}
printf ("\n");
etc...
}