Artificial intelligent assistant

How to sort a collection of lines from different files? I want to sort second line in each file by second column, and also print name of the corresponding file. I am doing it like this - rm tmp; for filename in file*; do num=`head -2 $filename | tail -1 | awk '{print $2}'`; echo "$filename $num" >> tmp; done; sort -n -k2,2 tmp Can it be done in a better way that doesn't include a `tmp` file?

Try:


$ awk 'FNR == 2' file1 file2 filen | sort -n -k2,2


With `gawk`, you can use `nextfile` for efficience:


$ gawk 'FNR == 2 {print FILENAME,$2; nextfile}' file1 file2 filen | sort -n -k2,2


or you can write your own `nextfile` function in other `awk` implementation refer to this.

If you don't have `gawk`, you can use `perl` for more portable:


$ perl -anle 'print "$ARGV $F[1]" and close ARGV if $. == 2' file1 file2 filen |
sort -n -k2,2

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 357bb15607b89e03e1209d94259d9de6