The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need help in sort ali560045 Shell Programming and Scripting 2 12-04-2007 07:38 AM
sort prasathlogu UNIX for Dummies Questions & Answers 1 10-08-2007 06:56 AM
du -h | sort ? fongthai Shell Programming and Scripting 6 11-02-2006 08:59 PM
How do i sort? abhijeetkul Shell Programming and Scripting 1 12-22-2005 03:49 AM
2nd sort key whatisthis UNIX for Dummies Questions & Answers 1 10-20-2004 10:46 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 03-16-2007
Indalecio Indalecio is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sweden
Posts: 59
sort -k

Hi all

Iīm stuck with the sort command and would like to ask you for help.
Iīm using the following command on the following file:

Code:
sort -t "|" -k 1 -k 9 -k 17r file

file:
21833  |23901602|01|2| |                |1|0056047507|N| | | | | | | |IN|
...
21837  |20058199|01|6| |                |1|0003110782|N| | | | | | | |CR|
21837  |21500283|01|2| |                |1|0006044233|Y| | | | | | | |CR|
21837  |23688532|01|2| |                |1|0038096691|N| | | | | | | |IN|
21837  |23759215|01|2| |                |1|0044596008|Y| | | | | | | |IN|
...
21872  |23809251|01|2| |                |1|0048651199|N| | | | | | | |IN|
22224  |20085858|01|4| |                |1|0003180165|N| | | | | | | |IN|
aaaaa  |24053243|01|2| |                |1|0068367876|N| | | | | | | |IN|
What I want is a file sorted by the first field (the original file is but I want to keep this as my primary sorting while sorting by other criterions). Then I want the ninth field (N or Y) to be sorted and then the last one (IN/CR) in reverse order (so IN comes before CR).

The expected result is the following (check the four lines in the middle):

Code:
21833  |23901602|01|2| |                |1|0056047507|N| | | | | | | |IN|
...
21837  |23688532|01|2| |                |1|0038096691|N| | | | | | | |IN|
21837  |20058199|01|6| |                |1|0003110782|N| | | | | | | |CR|
21837  |23759215|01|2| |                |1|0044596008|Y| | | | | | | |IN|
21837  |21500283|01|2| |                |1|0006044233|Y| | | | | | | |CR|
...
21872  |23809251|01|2| |                |1|0048651199|N| | | | | | | |IN|
22224  |20085858|01|4| |                |1|0003180165|N| | | | | | | |IN|
aaaaa  |24053243|01|2| |                |1|0068367876|N| | | | | | | |IN|
Why is my command not working?

Cheers

Last edited by Indalecio; 03-16-2007 at 03:39 AM..
  #2 (permalink)  
Old 03-16-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sort -t "|" -k 1.1,1.7 -k 9.1,9.1 -k 17.1,17.2r file
  #3 (permalink)  
Old 03-16-2007
Indalecio Indalecio is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sweden
Posts: 59
Thanks anbu, but this doesn't work. The second sorting rule is not applying:

Code:
21833  |23901602|01|2| |                |1|0056047507|N| | | | | | | |IN|
...
21837  |23688532|01|2| |                |1|0038096691|N| | | | | | | |IN|
21837  |23759215|01|2| |                |1|0044596008|Y| | | | | | | |IN|
21837  |20058199|01|6| |                |1|0003110782|N| | | | | | | |CR|
21837  |21500283|01|2| |                |1|0006044233|Y| | | | | | | |CR|
...
21872  |23809251|01|2| |                |1|0048651199|N| | | | | | | |IN|
22224  |20085858|01|4| |                |1|0003180165|N| | | | | | | |IN|
aaaaa  |24053243|01|2| |                |1|0068367876|N| | | | | | | |IN|
Using -k 9 makes the rule work, but then the third fails...
  #4 (permalink)  
Old 03-16-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,953
and this,

Code:
sort -t "|" -k 1.1,1.8 -k 9.1,9.1 -k 17.1,17.2r  filename
and verified the output that I got with your expected output and it looks same!

  #5 (permalink)  
Old 03-16-2007
Indalecio Indalecio is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sweden
Posts: 59
Thatīs great, thanks both for your help!
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 05:23 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