Sort complex data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort complex data
# 1  
Old 02-10-2009
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  
Old 02-10-2009
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  
Old 02-10-2009
Question

Are you sorting each line by the number in () in descending order?
# 4  
Old 02-10-2009
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!
# 5  
Old 02-10-2009
Quote:
Originally Posted by joeyg
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!
# 6  
Old 02-10-2009
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)
$

# 7  
Old 02-10-2009
It failed at line "ARR=( $LINE )".
syntax error at line 5: `ARR=' unexpected
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk in complex number data

Hi, I'm trying to transform my data from the following format: eps:, 0.248281687841641, -2.83539034165844e-7, 2.78042576353472+6.3505226053266e-6i to this: eps:, 0.248281687841641, -2.83539034165844e-7, 2.78042576353472, +6.3505226053266e-6 so I can plot it with GnuPlot. how do I... (4 Replies)
Discussion started by: rogeriogouvea
4 Replies

2. Shell Programming and Scripting

Need to sort out data

Hello All, I have one file with multiple lines records like as below.. I need to extract only BFG and corresponding BSG record/line. for evry BFG there is one BSG record is there as mentioned in BOLD and so on... BFG BR 00001 20140724 000 000 ? ? BLG UVR QPR 01 380 ? ? 999 0 0 0 ? BLC... (2 Replies)
Discussion started by: Riverstone
2 Replies

3. Shell Programming and Scripting

How to sort by complex algorithm

Hello, To simplify ma question, here is my list : # cat liste a m x h and here is the right order to list his component : liste_order="1:m 2:a 3:h 4:x" The only way to sort my file like I want, I find this idea : cat liste | sed 's/a/2:a/g' | sed 's/m/1:m/g' | sed... (9 Replies)
Discussion started by: mlaiti
9 Replies

4. Shell Programming and Scripting

extract complex data from html table rows

I have bash, awk, and sed available on my portable device. I need to extract 10 fields from each table row from a web page that looks like this: </tr> <tr> <td>28 Apr</td> <td><a... (6 Replies)
Discussion started by: rickgtx
6 Replies

5. UNIX for Dummies Questions & Answers

sort data

Hi, I need to filter the output of a command and display certain data only. How can i do this ? My file contain: $ cat abc.txt <testcase title="AAA_100"> <testcase title="BBB_200"> <testcase title="CCC_300"> <testcase title="DDD"> ... (3 Replies)
Discussion started by: crazydude80
3 Replies

6. UNIX for Dummies Questions & Answers

Gathering data from complex/large dataspreads .txt format

Hi, I'm working on gathering information stored in .txt files. The format of the data within the .txt files is shown in the picture uploaded with this post. Sections like the one pictured are repeated (with different data, same format) many times within each .txt file but each section is of data... (4 Replies)
Discussion started by: p43hd
4 Replies

7. Shell Programming and Scripting

Trying to sort data

Im trying to sort all this data. I need to get a list out of the data (websites) and just list them out can anyone point me in the right direction. Im working with dans guardian. 2009.6.10 6:26:50 - 192.168.42.200... (5 Replies)
Discussion started by: darknirvana
5 Replies

8. UNIX for Dummies Questions & Answers

Sort directory with complex numeric file names

I have a directory with a large number (1000s) of files and I need to produce a file listing all the files in the directory ordered "properly" (properly will be explained shortly). The files have the following naming pattern: bul_13_5_228_b.txt bul_1_3_57.txt bul_13_6_229.txt... (2 Replies)
Discussion started by: fdsayre
2 Replies

9. Shell Programming and Scripting

Figure out complex sort

This is an extension to a question that was earlier posted on this forum: I have task in which I need to pickup a set of files from a directory depending on the following criteria: Every month 6 files are expected to arrive at /test. The files come with date timestamp and the latest file set... (7 Replies)
Discussion started by: w020637
7 Replies

10. Shell Programming and Scripting

sort data

Hi! I'm trying to sort a file.dat with the sort command. The data contained by file.dat is similar to the data set below: 100.000 99.000 110.000 55.000 113.000 33.000 25.000 9.000 15.000 It is relatively easy to sort the data in ascending or descending order, but the problem is... (11 Replies)
Discussion started by: bjorb
11 Replies
Login or Register to Ask a Question