LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-03-2010, 09:43 AM   #1
noony123
Member
 
Registered: Oct 2010
Posts: 167

Rep: Reputation: 0
Sorting file contents !


Hi all.

I have a file that contains strings similar to below

SPabcasc12
SPeoisdl09
SPeuiskl11
etc...

I want to sort the above strings according to the last (bolded) numbers. Like in above case, following should be sorted output

SPeoisdl09
SPeuiskl11
SPabcasc12

Can someone guide me how to do this ?
 
Old 12-03-2010, 09:53 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
sort -k1.9n file
that means to sort numerically the first field starting at position 9. This assumes that all the strings have the same format: 8 characters followed by a number.
 
1 members found this post helpful.
Old 12-03-2010, 09:55 AM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Here's a way - not the most efficient, but it works:
Code:
sed 's/\(.*[^0-9]\)\([0-9]*\)/\1 \2/' input_file | sort -gk2 | sed 's/ //'
It assumes a quantity of alphabetical characters, followed by digits.

colucix' idea above is very nice (thanks for that colucix!) and is something I didn't know about with `sort` - but as he mentioned, it is strict on the number of characters.
 
Old 12-03-2010, 10:06 AM   #4
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
Sir it really worked, but if possible, can you refer me an article which explains sort in the way you used above ?

i really didnt get this

-k1.9n

Kindly guide me.

Thanks alot Sir and also GrapefruiTgirl, i really appreciate your prompt response. Its all you guys help that motivates me to continue on linux.
 
Old 12-03-2010, 10:14 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
It is explained in man sort:
Code:
       -k, --key=POS1[,POS2]
              start a key at POS1, end it at POS2 (origin 1)


       POS  is  F[.C][OPTS], where F is the field number and C the character position in the field.  OPTS is
       one or more single-letter ordering options, which override global ordering options for that key.   If
       no key is given, use the entire line as the key.
See the format highlighted in red: in my example F is 1 for the first field, C is 9 for the character position (origin 1) and OPTS is n for numerical sort.
 
Old 12-03-2010, 10:22 AM   #6
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
Thanks alot. I got it now.

Thanks :-)
 
Old 12-03-2010, 10:30 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sorting a Dictionary File dougp25 Programming 4 11-02-2010 01:29 AM
Problem with sorting contents in a file - "10" being put up the top onesikgypo Programming 4 08-25-2009 07:19 AM
Sorting Rows of a file iframe Linux - Software 16 05-28-2009 04:38 AM
Help with sorting a file Yogiz Linux - Newbie 5 12-08-2007 04:41 PM
File sorting problem Kerridis Linux - General 3 03-08-2004 10:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:16 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration