LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 04-23-2013, 11:10 AM   #1
MikeyCarter
Member
 
Registered: Feb 2003
Location: Orangeville
Distribution: Fedora
Posts: 440

Rep: Reputation: 31
Question Sorting a file based on a parameter


I have a file that has TEXT=value on column 5, 8 or 9. I need to sort the level+number in value field.

ie:

junk junk junk sort=first+10 junk
junk junk sort=first+38 junk junk
junk junk junk junk sort=first+08 junk

How do I sort such a beast.
 
Old 04-23-2013, 11:51 AM   #2
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
The example you gave has the desired column at 4, 3, and 5. ?
Does the desired column also have the vaule "second" as well as "first" (as you portray)?

Please post a clearer example of input and desired output. Thanks.
 
Old 04-23-2013, 11:53 AM   #3
MikeyCarter
Member
 
Registered: Feb 2003
Location: Orangeville
Distribution: Fedora
Posts: 440

Original Poster
Rep: Reputation: 31
Current file:
junk junk junk sort=first+10 junk
junk junk sort=first+38 junk junk
junk junk junk junk sort=first+08 junk
junk junk junk sort=first+40 junk
junk junk sort=first+6 junk junk

What I want
junk junk sort=first+6 junk junk
junk junk junk junk sort=first+08 junk
junk junk junk sort=first+10 junk
junk junk sort=first+38 junk junk
junk junk junk sort=first+40 junk
 
Old 04-23-2013, 12:35 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


The sort command will most likely do what you want in any case. Read info sort and give it a try. Post back with the results of your attempts and we'll help you with any problems you still have.

Edit: No, looking again, actually it probably won't. If the fields to sort by are not all in the same column, then you'll need something more powerful, like awk or perl.

Last edited by David the H.; 04-23-2013 at 12:38 PM.
 
Old 04-23-2013, 12:57 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Ok, since this is non-trivial, here's a gawk solution for you. It relies on its new gensub function and internal sorting features, so you need gawk v4+.

Code:
awk 'BEGIN{ PROCINFO["sorted_in"]="@ind_num_asc" } \
     { a[gensub(/.*sort=first[+]([0-9]+).*/,"\\1","1")]=$0 }\
     END{ for (i in a) { print a[i] } }'  infile.txt
What it does is extract the number from the sorting fields, and uses them as the index numbers for an array that holds the whole line. Sort the array indexes and the when the array is printed out it will also be in the order you want.

Last edited by David the H.; 04-23-2013 at 01:09 PM.
 
Old 04-23-2013, 01:52 PM   #6
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
Looks like David the H beat me to it. I initially thought sort as well but realized the OP's needs were beyond sort's scope.
Nice solution with awk.
 
Old 04-23-2013, 09:04 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
I'd have used a Perl hash, where the reqd field is used as the hash key. Gets a tad more fiddly if dupe keys are allowed, but its still do-able
 
Old 04-25-2013, 10:27 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Pretty much any solution just requires extracting the desired number from each line, sorting by them, then printing out the lines they are associated with.

Simple arrays are generally the easiest way to do so, but yeah, they usually won't work if there are duplicate numbers involved. Later entries would overwrite earlier ones unless you went to special lengths to keep the indexes unique. And that would probably require some extra work with associative arrays/hashes.


But here's a simple shell loop that modifies the lines so that they can be processed with sort. It should handle duplicates safely and is posix-portable.

Code:
while read -r line || [ -n "$line" ] ; do
    num=${line##*first+}
    num=${num%% *}
    echo "$num $line"
done <input.txt | sort -k1n | cut -d' ' -f2-
The only remaining limitation is that, since it hasn't been defined how duplicate lines should be ordered in relation to each other, they will just come out in whatever way sort thinks they should. '-k1n' will simply sort the whole line alphanumerically, starting from the first field.
 
  


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
[SOLVED] Sorting filenames based on length of the file zama Linux - Software 5 03-26-2012 04:22 AM
[SOLVED] Sorting a file with a variable name juliamarie Linux - Newbie 1 06-27-2011 01:56 PM
How to display (printf) selective parameter based on conditions eryn Programming 10 08-26-2009 01:09 PM
Sorting photos into folders based on exif date SuperJediWombat! Programming 7 05-16-2009 06:53 PM
Help with sorting a file Yogiz Linux - Newbie 5 12-08-2007 04:41 PM


All times are GMT -5. The time now is 07:26 AM.

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