LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 11-01-2010, 05:53 PM   #1
dougp25
Member
 
Registered: Dec 2003
Location: Seacoast NH
Distribution: PCLinuxOS
Posts: 37

Rep: Reputation: 2
Sorting a Dictionary File


I have this dictionary file that has this format:

SUBSTITUTE_VALUE, Real Value
SUBSTITUTE_VALUE_2, Real Big Value
SUBSTITUTE_VALUE_HECL, Hardware Abstract
SUBSTITUTE_V, Valley Mem

I want to sort this file so the LONGEST Substitute Values are listed at the top. (Obviously I want to keep the related values tagging along with them)!

Playing with sort but that's not getting me there! Graciously asking for some help!
 
Old 11-01-2010, 06:22 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe this is not exactly what you're looking for (I guess you have a lot of different keys, not only "SUBSTITUTE_VALUE") but it may give you a clue:
Code:
awk -F, '{print length($1), $0}' file | sort -k1nr | cut -d' ' -f2-
 
Old 11-01-2010, 06:29 PM   #3
dougp25
Member
 
Registered: Dec 2003
Location: Seacoast NH
Distribution: PCLinuxOS
Posts: 37

Original Poster
Rep: Reputation: 2
That's pretty close! It's just kind of strange because here and there, there's a value that's shorter than the one above and below it... But it's very close!
It doesn't have to be an alphabetical sort based on the first field either, sorted, my example file should look like:

SUBSTITUTE_VALUE_HECL, Hardware Abstract
SUBSTITUTE_VALUE_2, Real Big Value
SUBSTITUTE_VALUE, Real Value
SUBSTITUTE_V, Valley Mem

SUBSTITUTE_V would still be the last entry, even if it looked like this

SUBSTITUTE_V, Valley Memory Field Leak Protector Hex FF
(since we don't really care about the value of field2)

Last edited by dougp25; 11-01-2010 at 06:31 PM.
 
Old 11-01-2010, 06:40 PM   #4
dougp25
Member
 
Registered: Dec 2003
Location: Seacoast NH
Distribution: PCLinuxOS
Posts: 37

Original Poster
Rep: Reputation: 2
Perfect, I got it! Thanks colucix! My field separator wasn't a comma, and I needed to redirect output to a new file, but pretty slick all the same!

Thanks again.
 
Old 11-02-2010, 01:29 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome! I'm glad to see it works as required.
 
  


Reply



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
sorting a file by column AutoC Programming 6 08-07-2009 05:43 PM
Suggest a good offline dictionary-database for gnome-dictionary. abhijeetnayak Linux - Software 1 07-05-2009 05:17 PM
Help with sorting a file Yogiz Linux - Newbie 5 12-08-2007 04:41 PM
Linux File Sorting linuxgodrh Linux - General 7 08-01-2007 09:33 AM
File sorting problem Kerridis Linux - General 3 03-08-2004 10:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 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