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
need_help need_help is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 6
Sort complex data

Hi,

Can someone here help sorting the following data in numeric order?

INPUT:
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)


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

Thanks in advance for your help!!!
  #2 (permalink)  
Old 02-10-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,960
That output is tricky, since it seems to be sorting in two different directions, alphabetically but reverse-numerically... commandline sort can't do that, you'd be writing your own shell-based sorting routines. Is the form of the output really that strict? What's the data for?
  #3 (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
Question

Are you sorting each line by the number in () in descending order?
  #4 (permalink)  
Old 02-10-2009
need_help need_help is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 6
Quote:
Originally Posted by joeyg View Post
Are you sorting each line by the number in () in descending order?
Yes. Need to sort numbers in descending order in each line.


Thank you!
  #5 (permalink)  
Old 02-10-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,960
Here's some code that could probably be more efficient but I think does what you want:

Code:
#!/bin/sh

while read TITLE LINE
do
        ARR=( $LINE )
        for ((N=0; N<${#ARR}; N++))
        do
                if [[ -z "${ARR[$N]}" ]]
                then
                        break
                fi

                OLDIFS="${IFS}"
                IFS="()"
                VAL=( ${ARR[$N]} )

                printf "%s %s\n" ${VAL[1]} ${VAL[0]}
                IFS="${OLDIFS}"
        done | sort -rn | (
                echo -n $TITLE
                while read I STR
                do
                        echo -n " ${STR}(${I})"
                done
                echo    )
done

exit 0
Code:
$ echo "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)" | ./sorter.sh
FIRST def(13) ijk(6) abc(3) abc(2) fgh(1)
SECOND hij(19) hub(10) dfe(10) abc(4) tlm(1) hij(1) abc(1)
OTHERS bub(19) abc(15) hij(10) mok(4) abc(2) hij(1) abc(1)
$
  #6 (permalink)  
Old 02-10-2009
need_help need_help is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 6
The data is from CVS log and I only care about the number, not alphabetic.
All I want to do is sort numers in each row.

Thanks!
  #7 (permalink)  
Old 02-10-2009
need_help need_help is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 6
It failed at line "ARR=( $LINE )".
syntax error at line 5: `ARR=' unexpected
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 03:44 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