Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have just started to learn unix & I need help with a task. It is not about scoring a mark in my assignment or something, but I am curious to know the answer.
There is a file with words such as alpha, bravo, charlie,.......zulu to start with all 26 alphabets. The task is to sort them in alphabetical order, but number them in reverse! The output should be:
26 alpha
25 bravo
24 charlie
.
.
.
1 zuluI tried the following commands:
sort < filename | nl -Both numbers & words sorted in ascending order
sort < filename -r | nl -Words sorted in reverse & numbered straight
nl < filename | sort -r -Words not sorted but numbering is reversedCan anyone please tell me how to achieve the desired output? Thanks.

It's a kludge, but the only way I can think to do it is to create a file of numbers sorted in reverse, reverse the datafile, and then paste the two files together:
cat -n datafile |sort -k 1,1nr |awk ' { print $1 } ' > fileno
sort datafile > myfile
paste fileno myfile

#!/usr/bin/ksh x=26 sort infile | while read lines;do print $x $lines x=$(($x-1)) done
your data resides in infile.
The script reads each line then prints the line, then the number defined in x. x starts at 26 and decreases one each line.Or, you can also use:
sort -r infile | nl | sort -k 2

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |