The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Ascending order within text kerpm Shell Programming and Scripting 5 08-15-2008 06:39 AM
Display modified files in ascending order balareddy UNIX for Advanced & Expert Users 1 08-13-2008 02:48 AM
Ascending & Descending order numbers pravani1 Shell Programming and Scripting 6 06-03-2008 04:43 AM
use of sed over cat to merge files miwinter UNIX for Advanced & Expert Users 2 11-28-2007 10:36 AM
Sort / ascending order gyik UNIX for Dummies Questions & Answers 1 03-05-2001 07:08 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-03-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
merge two files in ascending order

Hello Friends,

I want to merge two files in ascending order on the first field. And if the first field matches sort on 3rd field i.e, TXADDR should come ahead of RXADDR .

file1
Code:
      9 : TXADDR  : 00000000
     65 : TXDATA  0000000000000011
     83 : TXDATA  0000000000000012
    453 : TXADDR  : 00000000
    509 : TXDATA  0000000000000001
    527 : TXDATA  0000000000000002
    879 : TXADDR  : 00000020
     934 : TXDATA  0000000000000011
     953 : TXDATA  0000000000000012
     971 : TXDATA  0000000000000013
file2
Code:
        9 : RXADDR  : 00000001
      65 : RXDATA  0000000000000011
      83 : RXDATA  0000000000000012
     102 : RXDATA  0000000000000013
     453 : RXADDR  : 00000000
     509 : RXDATA  0000000000000001
     527 : RXDATA  0000000000000002
     546 : RXDATA  0000000000000003
     879 : RXADDR  : 00000020
     934 : RXDATA  0000000000000011
     953 : RXDATA  0000000000000012
     971 : RXDATA  0000000000000013
     990 : RXDATA  0000000000000014
WIth the below command I able to sort and merge on the first field.
Code:
sort -n file1 file2 > file3
In file3 I expect when the first column matches it should give priority to the 3rd column i.e RXADDR & RXDATA.
So in my case the output should be
Code:
      9 : TXADDR  : 00000000
      9 : RXADDR  : 00000001
    65 : TXDATA  0000000000000011
    65 : RXDATA  0000000000000011
I dont know how sort with multiple key with sort pls suggest a option for that. or any other soln.

Regards,
user_prady

Last edited by user_prady; 09-03-2008 at 09:17 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 09-03-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 920
The following will sort first by the first key, numerically, and then by the second key alphabetically:

Code:
sort -k1,1n -k2,2 file1 file2 > file3
Reply With Quote
  #3  
Old 09-03-2008
RahulJoshi's Avatar
Registered User
 

Join Date: Aug 2008
Location: PUNE
Posts: 85
use this code:
sort -n -k 1 my1 my2
Reply With Quote
  #4  
Old 09-03-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 920
Actually, you'll want -k3,3 not -k2,2... but you probably figured that out yourself!
Reply With Quote
  #5  
Old 09-03-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by RahulJoshi View Post
use this code:
sort -n -k 1 my1 my2
Thanks for your kind replies , but both the command outputs the same as
the command
Code:
sort -n file1 file2 > file3
I want to reverse the third column so that TXADDR/TXDATA comes before RXADDR/RXDATA when the first column matches . The above commands gives me the reverse..
Reply With Quote
  #6  
Old 09-03-2008
Registered User
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by Annihilannic View Post
The following will sort first by the first key, numerically, and then by the second key alphabetically:

Code:
sort -k1,1n -k2,2 file1 file2 > file3
Thanks for your reply But I want to sort reverse for the second key ..
Reply With Quote
  #7  
Old 09-03-2008
dj -------
 

Join Date: Feb 2007
Location: Cochin/Bangalore, India
Posts: 439
Code :

Code:
sort -t":" -k1,1n -k2,2 file1 file2
Output:
Quote:
9 : RXADDR : 00000001
9 : TXADDR : 00000000
65 : RXDATA 0000000000000011
65 : TXDATA 0000000000000011
83 : RXDATA 0000000000000012
83 : TXDATA 0000000000000012
102 : RXDATA 0000000000000013
453 : RXADDR : 00000000
453 : TXADDR : 00000000
509 : RXDATA 0000000000000001
509 : TXDATA 0000000000000001
527 : RXDATA 0000000000000002
527 : TXDATA 0000000000000002
546 : RXDATA 0000000000000003
879 : RXADDR : 00000020
879 : TXADDR : 00000020
934 : RXDATA 0000000000000011
934 : TXDATA 0000000000000011
953 : RXDATA 0000000000000012
953 : TXDATA 0000000000000012
971 : RXDATA 0000000000000013
971 : TXDATA 0000000000000013
990 : RXDATA 0000000000000014
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0