Computing.Net > Forums > Unix > Command help!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Command help!

Reply to Message Icon

Name: Bill-B
Date: May 15, 2009 at 05:10:55 Pacific
OS: Unix
Subcategory: General
Comment:

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 zulu

I 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 reversed

Can anyone please tell me how to achieve the desired output? Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 15, 2009 at 06:38:11 Pacific
Reply:

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


0

Response Number 2
Name: lankrypt0
Date: May 15, 2009 at 09:04:39 Pacific
Reply:

#!/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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Command help!

unix command help www.computing.net/answers/unix/unix-command-help/6321.html

tar command help www.computing.net/answers/unix/tar-command-help/2966.html

SED Command help www.computing.net/answers/unix/sed-command-help/4277.html