Reputation: 2593
I am trying to find a file with in a dir that contains the largest number (at position 3rd row 3rd column). I want both the max value and the file name containing max value printed. This is what i have right now
find ./ -name sample-file\* -exec sed '3!d' {} \; | awk '{print $3}' | awk 'n<$1{n=$1}END{print n}'
This gets me the max value, but i also want the file name containing the max value. Print along with this.
Current output:
When run for dir1:
487987
When run for dir2:
9879889
I want the output to be like this
when run for dir1:
file16 487987
when run for dir2:
file23 9879889
Appreciate some inputs on this. Thanks
Upvotes: 0
Views: 602
Reputation: 80931
awk script:
BEGIN {
n = 0
fn = ""
}
(FNR == 3) && ($3 > n) {
n = $3
fn = FILENAME
}
END {
printf "%s: %s\n", fn, n
}
use as awk -f <file.awk> sample-file*
.
Could probably be more efficient with nextfile
after the fn
assignment in the FNR block or similar mechanisms to short-circuit the rest of the other lines in each input file.
zcat and shell
declare n=0 fn=
for i in sample-file*; do
t=$(zcat "$i" | awk 'NR == 3 {print $3; exit}')
if [ $t -gt $n ]; then
n=$t
fn=$i
fi
done
echo "$fn: $n"
Upvotes: 3