Sort command in one column and not effect to another column | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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 !!

Sort command in one column and not effect to another column

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-22-2013
GeodusT GeodusT is offline
Registered User
 
Join Date: May 2011
Last Activity: 24 March 2013, 10:47 PM EDT
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
Sort command in one column and not effect to another column

If my data is numerical :


Code:
1 = 101
2 = 102
3 = 104
4 = 104
7 = 103
8 = 103
9 = 105

I need the result like below:


Code:
1 = 101
2 = 102
3 = 103
4 = 103
7 = 104
8 = 104
9 = 105


I just want to sort the data in column 3 only doesn't another column change.

How about the command I should using?

Thank you.

Last edited by radoulov; 03-22-2013 at 07:39 AM..
Sponsored Links
    #2  
Old 03-22-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 September 2014, 9:50 PM EDT
Location: NM
Posts: 10,211
Thanks: 278
Thanked 796 Times in 743 Posts
Try:

Code:
awk '{print $2, $3}' inputfile | sort | nl > newfile

Edit: forgot field 3

Last edited by jim mcnamara; 03-22-2013 at 08:01 AM..
The Following User Says Thank You to jim mcnamara For This Useful Post:
GeodusT (03-22-2013)
Sponsored Links
    #3  
Old 03-22-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 20 September 2014, 5:20 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,674
Thanks: 182
Thanked 620 Times in 578 Posts

Code:
perl -F'\s*=\s*' -lane'
  push @f, $F[0];
  push @s, $F[1];
  END {
    @ss = sort { $a <=> $b } @s;
    for (sort { $a <=> $b } @f) {
      printf "%s = %s\n", $_, shift @ss 
      }    
    }' infile

Or (if your shell supports process substitution):


Code:
paste -d= <(cut -d= -f1 infile | sort -n) <(cut -d= -f2 infile | sort -n)


Last edited by radoulov; 03-22-2013 at 08:09 AM..
The Following User Says Thank You to radoulov For This Useful Post:
GeodusT (03-22-2013)
    #4  
Old 03-22-2013
GeodusT GeodusT is offline
Registered User
 
Join Date: May 2011
Last Activity: 24 March 2013, 10:47 PM EDT
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
@JIM code doesn't work. thank you

@Radoulov your code is work. thank you
Sponsored Links
    #5  
Old 03-22-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 September 2014, 9:50 PM EDT
Location: NM
Posts: 10,211
Thanks: 278
Thanked 796 Times in 743 Posts
Works now. Please see the edit to the answer.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk command to print only selected rows in a particular column specified by column name ks_reddy Shell Programming and Scripting 4 09-01-2012 11:32 AM
command for converting 4 column data to 1 column rpf Shell Programming and Scripting 5 12-12-2011 10:40 AM
sort on second column only based on first column malcomex999 Shell Programming and Scripting 7 03-15-2010 10:46 AM
Question about sort specific column and print other column at the same time ! patrick87 Shell Programming and Scripting 3 10-07-2009 06:43 AM
Sort by the third column with +n command bobo UNIX for Dummies Questions & Answers 2 08-23-2006 04:10 PM



All times are GMT -4. The time now is 06:07 AM.