Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

sort on second column only based on first column

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-15-2010
malcomex999 malcomex999 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 10 November 2015, 2:16 AM EST
Location: In the beautiful World...
Posts: 511
Thanks: 10
Thanked 29 Times in 29 Posts
sort on second column only based on first column

I have an input file like this...

Code:
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Sunlight
AAAlkalines     Sunlight
AAAlkalines     Sunlight
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAASalines      Energizer
AAASalines      Energizer
AAASalines      Sunlight
Batteries       Energizer
Batteries       Sunlight
Batteries       Energizer
RechargableAAA  Energizer
RechargableAAA  Energizer
RechargableAAA  Duracell
RechargableAAA  Duracell
EmergencyLight  AlFaris
EmergencyLight  AlFaris
EmergencyLight  Geepas
EmergencyLight  Geepas

I want to get the output as below while the first column is intact(without moving the order) but sorting on the second column only according to the names in the first column...

Code:
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Sunlight
AAAlkalines     Sunlight
AAAlkalines     Sunlight
AAASalines      Energizer
AAASalines      Energizer
AAASalines      Sunlight
Batteries       Energizer
Batteries       Energizer
Batteries       Sunlight
RechargableAAA  Duracell
RechargableAAA  Duracell
RechargableAAA  Energizer
RechargableAAA  Energizer
EmergencyLight  AlFaris
EmergencyLight  AlFaris
EmergencyLight  Geepas
EmergencyLight  Geepas

Sponsored Links
    #2  
Old Unix and Linux 03-15-2010
clx clx is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 22 September 2017, 7:11 AM EDT
Location: Mumbai, India
Posts: 1,690
Thanks: 138
Thanked 205 Times in 199 Posts
if the sequence of the col1 values are not important,


Code:
for col1 in $(awk '{print $1}' file | sort -u); do grep "^$col1" file | sort -k2; done


o/p:

Code:
AAASalines      Energizer
AAASalines      Energizer
AAASalines      Sunlight
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Energizer
AAAlkalines     Sunlight
AAAlkalines     Sunlight
AAAlkalines     Sunlight
Batteries       Energizer
Batteries       Energizer
Batteries       Sunlight
EmergencyLight  AlFaris
EmergencyLight  AlFaris
EmergencyLight  Geepas
EmergencyLight  Geepas
RechargableAAA  Duracell
RechargableAAA  Duracell
RechargableAAA  Energizer
RechargableAAA  Energizer

Sponsored Links
    #3  
Old Unix and Linux 03-15-2010
malcomex999 malcomex999 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 10 November 2015, 2:16 AM EST
Location: In the beautiful World...
Posts: 511
Thanks: 10
Thanked 29 Times in 29 Posts
Actually, the sequence of the first column is very important to me.
    #4  
Old Unix and Linux 03-15-2010
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 9 January 2017, 4:40 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 Posts

Code:
perl -lane'
  $_{$F[0]}++ or $c++;
  push @x, [$c, @F];
  END {
    print $_->[1], "\t", $_->[2] for 
      sort {
        $a->[0] <=> $b->[0] || 
        $a->[2] cmp $b->[2]
        } @x
    }' infile

Sponsored Links
    #5  
Old Unix and Linux 03-15-2010
malcomex999 malcomex999 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 10 November 2015, 2:16 AM EST
Location: In the beautiful World...
Posts: 511
Thanks: 10
Thanked 29 Times in 29 Posts
Quote:
Originally Posted by radoulov View Post
Code:
perl -lane'
  $_{$F[0]}++ or $c++;
  push @x, [$c, @F];
  END {
    print $_->[1], "\t", $_->[2] for 
      sort {
        $a->[0] <=> $b->[0] || 
        $a->[2] cmp $b->[2]
        } @x
    }' infile

Well, i don't have perl on the system i am working right now but i will give it a try later on another system.

Thanks,
Sponsored Links
    #6  
Old Unix and Linux 03-15-2010
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Advisor  
Forum Adviser
 
Join Date: Jan 2007
Last Activity: 9 January 2017, 4:40 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 629 Times in 586 Posts
Ok Linux


Code:
awk '$1 != p { c++ }
{ print c, $0; p = $1 }
' p=1 infile |
  sort -k1.1n -k3.3 |
    cut -d\  -f2-

Sponsored Links
    #7  
Old Unix and Linux 03-15-2010
malcomex999 malcomex999 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 10 November 2015, 2:16 AM EST
Location: In the beautiful World...
Posts: 511
Thanks: 10
Thanked 29 Times in 29 Posts
LinuxLinux Yes, that's it. It is working perfect.

Thanks,
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read CSV column value based on column name JohnGG Shell Programming and Scripting 3 11-20-2009 12:33 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
name the column based on symbols in another column repinementer Shell Programming and Scripting 7 08-27-2009 12:37 AM
Change names in a column based on the symbols in another column repinementer Shell Programming and Scripting 8 08-14-2009 03:30 AM
column to rows based on another column... malcomex999 Shell Programming and Scripting 5 07-08-2009 06:28 AM



All times are GMT -4. The time now is 07:33 PM.