Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have multiple files:

File 1

a 1 2

b 3 2

File 2

a 2 2 3

b 4 3 3

File 3

d 3 1 2 4

e 4 1 1 2

I'd like to sort the files according to the column next to the last column, after sort:

File 1

a 3 2

b 1 2

File 2

a 2 3 3

b 4 2 3

File 3

d 3 1 2 4

e 4 1 1 2

I'm using awk:

while read line
do
cat $line|awk '{print $(NF-1)"\t"$0}'|\
         sort -n -r|awk '$1=" " {print $0}' > $line.sorted.txt
done < listfile.txt

Is there any better way to do that? (Using awk, there are some problems when n (columns) is large)

Thanks,

share|improve this question

Try with something like

for i in f*; do
  echo "=== $i" ;
  N=$( echo $( head -1 $i | wc -w ) -1 | bc );
  sort -nrk$N $i;
done
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .