The UNIX and Linux Forums  

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
Figure out complex sort w020637 Shell Programming and Scripting 7 02-05-2009 03:13 PM
sort data in different columns mogabr Shell Programming and Scripting 15 08-04-2008 07:07 AM
Manage Complex XML Data in Oracle XML DB 11g iBot Oracle Updates (RSS) 0 04-06-2008 06:10 AM
Script to sort data wizardy_maximus Shell Programming and Scripting 1 11-21-2007 04:30 AM
sort data bjorb Shell Programming and Scripting 11 09-15-2005 06:22 PM

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 02-10-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,960
Try /bin/bash instead of /bin/sh. I keep forgetting that plain sh doesn't always support "advanced" things like.. sigh.. arrays..
  #2 (permalink)  
Old 02-10-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Here is a little scripting to accomplish this

Code:
> cat file167
FIRST abc(3) def(13) fgh(1) ijk(6) abc(2)
SECOND dfe(10) abc(4) hij(19) tlm(1) hij(1) hub(10) abc(1) fed(3)
OTHERS hij(10) mok(4) bub(19) hij(1) abc(2) abc(15) abc(1) hij(3)

> cat sort167.sh
#!/usr/bin/bash

while read header data
   do
#   echo $header
#   echo $data
    revdata=`echo $data | tr " " "\n" | sort -rn +0.4 | tr "\n" " "`

    echo $header $revdata

done <file167

> sort167.sh
FIRST def(13) ijk(6) abc(3) abc(2) fgh(1)
SECOND hij(19) hub(10) dfe(10) abc(4) fed(3) tlm(1) hij(1) abc(1)
OTHERS bub(19) abc(15) hij(10) mok(4) hij(3) abc(2) hij(1) abc(1)
  #3 (permalink)  
Old 02-10-2009
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,892
With Perl:

Code:
perl -lane'
    print join " ", shift @F, sort {
        ($aa) = $a =~ /(\d+)/;
        ($bb) = $b =~ /(\d+)/;
        $bb <=> $aa
      } @F
  ' infile
  #4 (permalink)  
Old 02-12-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
hi, i think perl should be a right and easy solution.

Code:
#!/usr/bin/perl
sub sub_sort{
	my @t1=split("[(|)]",$_[0]);
	my @t2=split("[(|)]",$_[1]);
	if($t1[1] == $t2[1]){
		return $t1[0] cmp $t2[0];
	}
	else{
		return $t2[1] <=> $t1[1];
	}
}
open FH,"<a.txt";
while(<FH>){
	chomp;
	my @temp=split(" ",$_);
	print $temp[0]," ", join " ",sort { sub_sort($a,$b) } @temp[1..$#temp];
	print "\n";
}
  #5 (permalink)  
Old 02-12-2009
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,892
Nice catch summer_cherry, I did not notice that the OP wants to sort twice (first numeric, then ascii/utf8), so I need to modify the code:

Code:
perl -lane'
    print join " ", shift @F, sort {
        ($aa) = $a =~ /(\d+)/;
        ($bb) = $b =~ /(\d+)/;
        $bb <=> $aa || $a cmp $b
      } @F
  ' infile
  #6 (permalink)  
Old 02-10-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,771
need_help - what shell are you using? show the result from
Code:
echo $SHELL
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 06:23 AM.


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