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?
Best Answer
Try:
With
gawk
, you can usenextfile
for efficience:or you can write your own
nextfile
function in otherawk
implementation refer to this.If you don't have
gawk
, you can useperl
for more portable: