Sort complex data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort complex data
# 8  
Old 02-10-2009
need_help - what shell are you using? show the result from
Code:
echo $SHELL

# 9  
Old 02-10-2009
Try /bin/bash instead of /bin/sh. I keep forgetting that plain sh doesn't always support "advanced" things like.. sigh.. arrays..
# 10  
Old 02-10-2009
Hammer & Screwdriver 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)

# 11  
Old 02-10-2009
With Perl:

Code:
perl -lane'
    print join " ", shift @F, sort {
        ($aa) = $a =~ /(\d+)/;
        ($bb) = $b =~ /(\d+)/;
        $bb <=> $aa
      } @F
  ' infile

# 12  
Old 02-12-2009
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";
}

# 13  
Old 02-12-2009
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

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