But is more efficient to only remember the current maximum and its field1, in two variables. Like RudiC did.
If you do not like the initialization MAX=-1E100, then you can do
awk '
NR==1 {MAX=$2; C1=$1}
{ for (x=2; x<=NF; x++) if ($x>MAX) { MAX=$x; C1=$1 } }
END { print C1, MAX }
' file
I have the following code set up to analyze several thousand files is multiple directories. The result output is only a single max out of all $file per $dir.
I am expecting a max value for each of the $file for each $dir to be returned.
What am I missing?
#!/bin/bash
for dir in a b c x y z
do
for file in USW*
do
awk '
BEGIN {MAX=-1E100
}
{for (x=2; x<=NF; x++) if ($x>MAX) {MAX = $x
C1 = $1
}
}
END {print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), substr(C1,20,8), MAX
}
' /dir/of/files/$dir/$file
done
done
As there's no file matching USW* in the current directory, the pattern is NOT expanded but given as is to the awk command: awk '...' a/USW* which then expands to ALL files in a and makes them all one single stream: awk '. . .' a/USW000004444.prpc.eq.0.str a/USW000004445.prpc.eq.0.str with one single MAX etc. value.
Try in lieu:
for dir in a b c
do for file in $dir/USW*
do awk '
BEGIN {MAX=-1E100
}
{for (x=2; x<=NF; x++) if ($x>MAX) {MAX = $x
C1 = $1
}
}
END {print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), substr(C1,20,8), MAX
}
' $file
done
done
USW00000444 Y199 M1 0PRCPEQ0 11
USW00000444 Y199 M1 0PRCPEQ0 6
awk: cannot open b/USW* (No such file or directory)
awk: cannot open c/USW* (No such file or directory)
(I guess your substr extracts one char too few...)