1752588 Members
4792 Online
108788 Solutions
New Discussion юеВ

sorting help urgent

 
SOLVED
Go to solution
viseshu
Frequent Advisor

sorting help urgent

I have an input file attached!!

I need the output to be sorted by the line number along with the record number as well.
(both line and record number are specified in the file).
for the lines where field number is not given, they shud be sorted along the line sorting.
6 REPLIES 6
Peter Godron
Honored Contributor
Solution

Re: sorting help urgent

Hi,
my solution would be delimiter replacement:
#!/usr/bin/sh
sed "s/- line/*/" data > data2
sed "s/,field/*/" data2 > data3
sort -t'*' -k 2,2n -k 3,3n data3 > data4
sed "s/*/- line/" data4 > data5
sed "s/*/,field/" data5 > data6
cat data6

If you input file is called data it substitues the "- line" with a "*" and repeats with ",field".
Then a sort is carried out to sort first on line, then on field.
Then the placeholders are replaced with their initial values.
You need to use a unique delimiter ! Base din your inputfile I used "*", you may need to use something else.

James R. Ferguson
Acclaimed Contributor

Re: sorting help urgent

Hi Viseshu:

Here's a simple solution:

# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;
my @list;
my $line;
my $field;
while (<>) {
$line = m/line\s(\d+)/ ? $1 : 0;
$field = m/field\s(\d+)/ ? $1 : 0;
push @list, sprintf "%6d%6d%s", $line, $field, $_;
}
@list = sort @list;
for $line (@list) {
print substr( $line, 12 );
}
1;

...Run this passing your input file to be sorted:

# ./filter file

Regards!

...JRF...
Sandman!
Honored Contributor

Re: sorting help urgent

Try the awk construct below...

awk -F"-" '{
gsub("[\",]"," ",$NF)
split($NF,z," ")
ml < z[2] ? ml=z[2] : ml
mr < z[4] ? mr=z[4] : mr
z[4] ? x[z[2]""z[4]]=$0 : x[z[2]""0]=$0
} END {
for (i=0;i for (j=0;j if (x[i""j])
print x[i""j]
}' file
Hemmetter
Esteemed Contributor

Re: sorting help urgent

Hi viseshu,

What about:
$sort -k9,9n -k10,10n data

rgds
HGH

Dennis Handly
Acclaimed Contributor

Re: sorting help urgent

A possible problem with Hemmetter's solution is that there are two formats for the messages. If there are any fewer words in the message, it will fail. A possibly safer sort command is:
$ sort -t- -k2,2 283372.txt

Unfortunately it does an ascii sort, not numeric. But Peter has solved that problem.

In Peter's solution, you can combine the first two seds and the last two:
#!/usr/bin/sh
sed -e 's/- line/*/' -e 's/,field/*/' data |
sort -t'*' -k2,2n -k3,3n |
sed -e 's/*/- line/"' -e 's/*/,field/'
James R. Ferguson
Acclaimed Contributor

Re: sorting help urgent

Hi:

I would respectively ask, "Didn't you find the other solutions with merit? Did you try any of them?

...JRF...