The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need immediate help with sorting!!! sasuke_uchiha Shell Programming and Scripting 9 08-06-2008 12:07 AM
trying to cope with awk difficulties amatuer_lee_3 Shell Programming and Scripting 8 05-11-2008 05:46 PM
Proxy ARP Difficulties TheMaskedMan IP Networking 7 11-02-2005 10:14 AM
Sorting mp3 penguin-friend Shell Programming and Scripting 3 05-13-2005 04:21 AM
Simple Network Program Difficulties Mistwolf High Level Programming 2 03-19-2002 06:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-21-2008
nthamma nthamma is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
Sorting Difficulties

Hey guys,

I am sort of new to unix and I am having difficulty sorting. What I am trying to do is sort a particular field in reversed order and another field in alphabetic order to create a new file that looks like this:

MILLER,EDWARD AL 14 101293 9341
MOORE,HENRY 15 101297 9077
NGUYEN,LE HUU 13 101314 9576

*NOTE THAT THE SPACINGS ARE NOT DISPLAYED CORRECTLY; IT SHOULD BE ALL LINED UP

here is what I tried:

sort -r +0.20 -0.24 +0.00 -0.16 "input file" > "output file"

Basically, "+0.20 -0.24" represents the first two digit numbers that needs to be reversed and "+0.00 -0.16" represents the names that needs to be in alphabetic order.

can someone help me?

Last edited by nthamma; 09-21-2008 at 03:40 PM..
  #2 (permalink)  
Old 09-21-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
why don't you use sort -k to specify the field and -t as field seperator
this is just an example..
Quote:
sort -t: +1 -2 -n +0 -1 -r filename
This command sequence performs a numeric sort on the second field (+1 -2 -n). Within that ordering, it sorts the first field in reverse alphabetic order (+0 -1 -r)

just go through man page of sort..
  #3 (permalink)  
Old 09-21-2008
nthamma nthamma is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
How do I specify the field?
  #4 (permalink)  
Old 09-21-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
if you have any field seperator like ',' you can go for -t option
or else try this...

Quote:
sort +0.00 -0.16 -d +0.20 -0.24 -r filename
  #5 (permalink)  
Old 09-21-2008
nthamma nthamma is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
it sorted the two digit numbers in reversed order which is what I want but instead of sorting the names in alphabetic order, it reversed it as well.

Last edited by nthamma; 09-21-2008 at 04:08 PM..
  #6 (permalink)  
Old 09-21-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
then you should specify some field seperator..
paste some part of input file and color the part which you wanna sort...

Last edited by vidyadhar85; 09-21-2008 at 04:24 PM..
  #7 (permalink)  
Old 09-21-2008
nthamma nthamma is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
I got it sorted!

What I did was give out this command:

sort-r +0.20 -0.24 -k 1,1d "input file" > "output file"

I could not specify the field for the two digit numbers because the names are not homogeneous. What I mean by that is that some of the names have 1 field and some of them have 2 fields:

Code:
MILLER,EDWARD AL   14 101293 9341
MOORE,HENRY        15 101297 9077
NGUYEN,LE HUU      13 101314 9576
You can see from above that the first and third name have 2 fields because of the middle name initials separated from the full names by a space. So, for those names, to get to the two digit numbers, you have to specify the 3rd field but for the name in the middle, you only have to specify up to the 2nd field.

So, what I did was count the spaces and use columns to specify the two digit numbers and reversed them. Next, I used the -k option to specify the 1st field for the names in alphabetic order using -d (sort in "phone directory" order). And it made my day! Took me so long to figure that one out and your replies really helped me understood how the whole thing works. Thanks for your help!

Last edited by Perderabo; 09-21-2008 at 05:32 PM.. Reason: Add code tags for readability
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0