0

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

4

1 回答 1

3

awk 脚本:

BEGIN {
    n = 0
    fn = ""
}

(FNR == 3) && ($3 > n) {
    n = $3
    fn = FILENAME
}

END {
    printf "%s: %s\n", fn, n
}

用作awk -f <file.awk> sample-file*.

在 FNR 块中分配或类似机制以短路每个输入文件中的其余其他行nextfile之后,可能会更有效。fn

zcat 和 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"
于 2013-11-12T03:16:49.177 回答