Shell Script that sorts data with number beginning on each line.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Shell Script that sorts data with number beginning on each line.
I need a shell script that sorts data alphabetically, and adds a number that begins on each line. I've seen it done a long time ago and I believe it was with the "sort" command..or maybe even wc -l. I've been looking everywhere and can't seem to find the solution.
Basically I need it to make a text file like this:
purple
yellow
red
blue
green
into this:
1:blue
2:green
3urple
4:red
5:yellow
Any help is greatly appreciated! I'm making a large database and like how numbers organize each listing in each category. ANY solution or suggestion would help greatly. Thanks!
THANKS!! It works wonderfully.
I'm still curious if its possible to get the output another way using sort.
I'm just opening the book "sed & awk" by oreilly 1990. Do you think its to old? Is GNUawk better than awk? Also how did the code you gave me tell the shell that it was gnu and not standard awk? Thanks again.
Quote:
Originally Posted by ghostdog74
if you have GNU awk:
Code:
awk ' { arr[c++] = $0}
END {
n = asort(arr,dest)
for (i=1;i<=n;i++) print i":"dest[i]
} ' "file"
Odds are, the awk you have is GNU awk.
Try entering awk --version and check the output. Mine says "GNU Awk 3.1.5..."
Your book is probably somewhat outdated; according to O'Reilly, their second edition (1997) was updated for The POSIX standard for sed and awk that was published after your 1990 book. I recommend you try using your book anyway and supplement it with online tutorials (one good one is http://www.grymoire.com/Unix/Awk.html, but there are others).
I recommend you use awk or perl because, once you know them, they can do much more for you than the bash shell. However, if you really want to use sort, you could do:
sort inputfile | cat -n | tr -s '\t' ' '
which would give you something close to what you asked for:
1 blue
2 green
3 purple
4 red
5 yellow
Thanks everbody....I learned the basics of array in awk and also the "cat -n " command....I used to do the same thing using nl command piping with sort, however, this does not number the spaces in the file.....
Sorry, but I'm just keeping my scripting skills away from rust
Sort, then count the lines, then loop and print the index along with the specific line using head & tail.
Code:
sort data > data.tmp
COUNT=`cat data.tmp | wc -l`
for i in `seq 1 $COUNT`
do
echo $i: `head -$i data.tmp | tail -1`
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.