LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-03-2012, 07:52 AM   #1
vostrushka
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Rep: Reputation: Disabled
sorting words with numbers


I have a text report that I need to sort by the first field.
That first field has letters and numbers, like this:
aaa1a bbb 1q1q1q1
aaa10a ccc 1q1q1q1
aaa2a ddd 1q1q1q1

As you can see the regular sorting will do exactly that. Line with "aaa10a" appears as second not third.
Number or characters before digits, for digits and after digits can be different.
Is it possible to make sorting right in such case and how?

Best regards
Leonid
 
Old 03-03-2012, 08:02 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Can you give us some actual examples? How will the programme decide which characters it needs to use to sort? You said the "Number or [of?] characters before digits, for digits and after digits can be different" - so what would you sort "a11b5a" by, 11 or 5? Is there always going to be 'aaa' before the number?

My suspicion is that you're going to have to use something like python, perl or awk to do it, unless there's more regularity to the data than you've implied.
 
Old 03-03-2012, 08:10 AM   #3
vostrushka
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
It is always alphabetic characters at the beginning, then digits and then alphabetic characters in the end.
I do not mind doing it in perl or awk. I never used python.

Leonid
 
Old 03-03-2012, 08:20 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
'sort -n' would work if the numbers were at the beginning. Do they always begin with the exact same sequence of letters 'aaa' ? If so, strip them off, sort, then add them back in.
 
Old 03-03-2012, 11:04 AM   #5
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,512

Rep: Reputation: 2657Reputation: 2657Reputation: 2657Reputation: 2657Reputation: 2657Reputation: 2657Reputation: 2657Reputation: 2657Reputation: 2657Reputation: 2657Reputation: 2657
Or you could

Script a way to strip out the sequences of alpha and numeric characters into a multidimensional array,
so from the array of lines, the line 'aaa10xx86bbz'
becomes array 'aaa 10 xx 86 bbz'
and sort the array using the identified key type for each field of the sub-array. (aaa alpha, 10 numeric, xx alpha, 86 numeric, bbz alpha)
then paste the sub-arrays back together by stripping spaces.

Simple in Pascal, Perl, or C. Somewhat challenging in scripting, but possible (make functions WORK for you), and the scripting approach would make use of sort using the field nomenclature (see the man page).

Easier of the pattern of alpha-numeric-alpha is always the same, but solvable either way. Just break down the steps.
 
1 members found this post helpful.
Old 03-03-2012, 01:25 PM   #6
vostrushka
LQ Newbie
 
Registered: Aug 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
This is good idea. Thank you. I'll try.
Leonid
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sorting output by a column with numbers AND letters. velocity_girl Linux - Newbie 13 06-09-2011 01:03 AM
Removing white spaces between words and joining the words in a given format Priyabio Linux - General 4 08-20-2009 07:42 AM
counting words in file and sorting them according to their frequency mystical dervish Programming 4 05-10-2007 08:58 AM
C programming - sorting random numbers Gigantor Programming 8 12-05-2005 10:32 PM
Sorting a list of words in LISP! Hady Programming 1 05-01-2004 03:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:46 PM.

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
Open Source Consulting | Domain Registration