LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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

Closed Thread
 
Search this Thread
Old 05-20-2006, 11:35 AM   #1
shmuel_orgad
LQ Newbie
 
Registered: May 2006
Posts: 5

Rep: Reputation: 0
sorting a file in shell script


HI ther. I nedd solving this problem.


The file i have contain the following fildes"

record #1: ipr05
record #2: inr02
record #3: ipr04
record #1: ipr03

I need to sort this file by the 2 last digits, ( 05, 02, 04, 03)after sorting the file the order of the record should like this:

record #1: inr02
record #2: ipr03
record #3: ipr04
record #1: ipr05

What is the command and the syntax for it ?

Thank's a lot

Shmuel
 
Old 05-20-2006, 12:04 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
The sort command will perform the function; 'man sort' will provide the documentation.
 
Old 05-20-2006, 12:10 PM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I guess you're looking for something like this:
Code:
echo -e "record #1: ipr05\nrecord #2: inr02" | sed -e "s/\(.*\)\([0-9]\{2\}\)$/\2 \1\2/" | sort | sed -e "s/^[0-9]\{2\} \(.*\)/\1/"
The first "sed" command looks for lines closing with two digits and copies the digits to the beginning of the line. Then "sort" sorts the lines before "sed" removes the leading digits again.
 
Old 05-20-2006, 01:36 PM   #4
shmuel_orgad
LQ Newbie
 
Registered: May 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Post sort a file i shell script

HI there.


The file i have contain the following fildes"

record #1: ipr05
record #2: inr02
record #3: ipr04
record #1: ipr03

I need to sort this file by the 2 last digits, ( 05, 02, 04, 03)after sorting the file the order of the record should like this:

record #1: inr02
record #2: ipr03
record #3: ipr04
record #1: ipr05

notes:
1. I don't know the numbers of records.
2. I don't know the name of the records.
3. I do know that the last 2 chracters are digits.

I need the general script structure for this problem.
What is the command and the syntax for it ?

Thanks
Shmuel Orgad
 
Old 05-20-2006, 01:39 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
So, use the sort command. Look at info sort for details and examples.
 
Old 05-20-2006, 01:57 PM   #6
shmuel_orgad
LQ Newbie
 
Registered: May 2006
Posts: 5

Original Poster
Rep: Reputation: 0
sort afile in script shell

HI there. I need solving this problem.


The file i have contain the following fildes"

record #1: ipr05
record #2: inr02
record #3: ipr04
record #1: ipr03

I need to sort this file by the 2 last digits, ( 05, 02, 04, 03)after sorting the file the order of the record should like this:

record #1: inr02
record #2: ipr03
record #3: ipr04
record #1: ipr05

notes:
1. I don't know the numbers of records.
2. I don't know the names of the records.
3. The records is allwayes 5 characters and the last 2 characters are digits.
I need the general solution for this problem.

What is the command and the syntax for it ?

Thank's a lot

Shmuel
 
Old 05-20-2006, 02:13 PM   #7
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
If your records are in a file called records, you can use the following to sort them using a sort starting at position 4:
Code:
cat records | sort --key=4
Have a look at man sort for more details
 
Old 05-20-2006, 02:35 PM   #8
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Does anyone know how often shmuel_orgad is going to ask this question?
 
Old 05-20-2006, 08:13 PM   #9
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Please do not post the same question more than one time.

http://www.linuxquestions.org/questi...d.php?t=446353
 
Old 05-20-2006, 08:14 PM   #10
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
duplicate post!
 
Old 05-20-2006, 08:15 PM   #11
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
That's a good question. This is number 4.

Closing.
 
Old 05-20-2006, 08:16 PM   #12
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Make that 5.
 
  


Closed Thread


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
shell script: File Manipulation simon_qwl Programming 2 10-29-2005 03:40 PM
file renaming with shell script or ? XJNick Linux - General 5 07-29-2005 02:43 PM
File Parsing using a Shell Script yasir15 Programming 5 08-22-2003 12:17 PM
File Maintenance in a shell script? mdscudder Linux - General 1 02-20-2003 05:14 PM


All times are GMT -5. The time now is 06:06 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 Google+: linuxquestions
Open Source Consulting | Domain Registration