Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer 1

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.