The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 02-10-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,972
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)
$