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 > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 10:39 AM
Display modified files in ascending order balareddy UNIX for Advanced & Expert Users 1 08-13-2008 06:48 AM
Ascending & Descending order numbers pravani1 Shell Programming and Scripting 6 06-03-2008 08:43 AM
use of sed over cat to merge files miwinter UNIX for Advanced & Expert Users 2 11-28-2007 01:36 PM
Sort / ascending order gyik UNIX for Dummies Questions & Answers 1 03-05-2001 10:08 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-03-2008
user_prady user_prady is offline
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-04-2008 at 01:17 AM..
  #2 (permalink)  
Old 09-04-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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
  #3 (permalink)  
Old 09-04-2008
RahulJoshi's Avatar
RahulJoshi RahulJoshi is offline
Registered User
  
 

Join Date: Aug 2008
Location: PUNE
Posts: 98
use this code:
sort -n -k 1 my1 my2
  #4 (permalink)  
Old 09-04-2008
user_prady user_prady is offline
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..
  #5 (permalink)  
Old 09-04-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 596
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
  #6 (permalink)  
Old 09-04-2008
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by dennis.jacob View Post
Code :

Code:
sort -t":" -k1,1n -k2,2 file1 file2
Output:
Oh oh . I think I am misguiding you all .

Desired Output :
Code:
9 : TXADDR : 00000000
9 : RXADDR : 00000001
65 : TXDATA 0000000000000011
65 : RXDATA 0000000000000011
83 : TXDATA 0000000000000012
83 : RXDATA 0000000000000012
102 : RXDATA 0000000000000013
453 : TXADDR : 00000000
453 : RXADDR : 00000000
509 : RXDATA 0000000000000001
509 : TXDATA 0000000000000001
527 : TXDATA 0000000000000002
527 : RXDATA 0000000000000002
546 : RXDATA 0000000000000003
879 : TXADDR : 00000020
879 : RXADDR : 00000020
....................
......................
again thanks a ton for your time..

Regards,
user_prady
  #7 (permalink)  
Old 09-04-2008
user_prady user_prady is offline
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 ..
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 09:43 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