LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-08-2007, 08:58 PM   #1
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Rep: Reputation: 0
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!
 
Old 05-08-2007, 09:44 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
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"
output:
Code:
# ./test.sh
1:blue
2:green
3:purple
4:red
5:yellow
 
Old 05-08-2007, 09:56 PM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,427

Rep: Reputation: 51
Or in perl:

Code:
#!/usr/bin/perl

my $filename="/tmp/test/file";
`cat $filename | sort > /tmp/test/sortedfile`;
open(FILE, "/tmp/test/sortedfile") || die "Could not open /tmp/test/sortedfile: $!\n";
$i=0;
while (<FILE>)
{
  print ++$i, ": $_";
}
close(FILE);
twantrd@twantrd:/tmp/test$ ./sort.pl
1: blue
2: green
3: purple
4: red
5: yellow


However, ghostdog's script looks better .

-twantrd
 
Old 05-08-2007, 09:59 PM   #4
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Original Poster
Rep: Reputation: 0
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"
output:
Code:
# ./test.sh
1:blue
2:green
3:purple
4:red
5:yellow
 
Old 05-08-2007, 10:25 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by sunksullen
THANKS!! It works wonderfully.
I'm still curious if its possible to get the output another way using sort.
Code:
awk '
{arr[c++] = $0}
END {
   for (word in arr) {
	print arr[word] | "sort"
   }
   close("sort")
}' "file"
Quote:
I'm just opening the book "sed & awk" by oreilly 1990. Do you think its to old?
If you ask me, no, its not. you can still learn the foundations.

Quote:
Is GNUawk better than awk?
To me, yes. As it has other useful features, like the inbuilt asort(), asorti() functions. plus others.

Quote:
Also how did the code you gave me tell the shell that it was gnu and not standard awk? Thanks again.
if you encounter error using the asort(), then most prob your awk version is not gawk. you can check your version , awk --version. Here's mine
Code:
awk --version
GNU Awk 3.1.5
Copyright (C) 1989, 1991-2005 Free Software Foundation.
 
Old 05-08-2007, 10:41 PM   #6
Valdis Grinbergs
LQ Newbie
 
Registered: Dec 2005
Distribution: Debian
Posts: 28

Rep: Reputation: 15
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
 
Old 05-08-2007, 11:15 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Quote:
Originally Posted by twantrd
Or in perl:

Code:
#!/usr/bin/perl

my $filename="/tmp/test/file";
`cat $filename | sort > /tmp/test/sortedfile`;
open(FILE, "/tmp/test/sortedfile") || die "Could not open /tmp/test/sortedfile: $!\n";
$i=0;
while (<FILE>)
{
  print ++$i, ": $_";
}
close(FILE);
twantrd@twantrd:/tmp/test$ ./sort.pl
1: blue
2: green
3: purple
4: red
5: yellow


However, ghostdog's script looks better .

-twantrd
Perl has internal sort function, so i think you could use that too, so there's no need to use external shell commands.
Code:
$file="file";
open(FH, "<$file") or die "Cannot open ";
# or @data=<FH>
while ( my $line = <FH> ) {
	chomp($line);
	push(@data,$line );
}
@sorted = sort @data;
foreach my $i (@sorted) {
	print $i ."\n";
}
 
Old 05-09-2007, 02:50 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
sort file | cat -n
Code:
billym.primadtpdev>sort 1|cat -n         
     1  blue
     2  green
     3  purple
     4  red
     5  yellow

Last edited by bigearsbilly; 05-09-2007 at 02:52 AM.
 
Old 05-09-2007, 05:42 AM   #9
livetoday
Member
 
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Rep: Reputation: 15
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.....
 
Old 05-09-2007, 12:13 PM   #10
sunksullen
LQ Newbie
 
Registered: May 2007
Location: Eugene, Oregon
Distribution: Ubuntu
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks so much everyone. All my questions were answered perfectly.
 
Old 05-09-2007, 03:11 PM   #11
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
a nice hack to the problem that nl is not using ":" is to first sort, number, then use awk with very simple action:
Code:
$ sort data | nl | awk '{print $1 ": " $2}'
OR with cat -n
Code:
$ sort data | cat -n | awk '{print $1 ": " $2}'
cheers,
-Mustafa
 
Old 05-09-2007, 03:28 PM   #12
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
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
-Mustafa
 
Old 05-09-2007, 03:35 PM   #13
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Rep: Reputation: 31


thanx "darsh" for your nice trick


#your friend tantawy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing beginning of line with a shell script oscarlevin Programming 9 05-07-2007 01:18 AM
how to program in shell to get a part of a file by line number George2 Linux - General 2 11-19-2006 02:41 AM
number of command line arguments to shell u4u Linux - General 1 03-04-2005 06:09 PM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 05:55 PM
Extracting the line number in Unix Shell? Aziz Programming 2 12-01-2004 11:03 AM


All times are GMT -5. The time now is 03:58 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration