Sorting a 2 column text file

Sorting a 2 column text file

Post by Marc » Tue, 11 Mar 2003 03:59:39



Have the following text file:

9.10:william wordsworth
9.60:william wordsworth
9.66:john doe
11.56:zachariah zebedee
11.70:adam a anaconda
12.32:john doe
15.00:john doe

which are race times for various people. I want to sort by the time column,
but only want the fastest time, i.e so that im left with

9.10:william wordsworth
9.66:john doe
11.56:zachariah zebedee
11.70:adam a anaconda

Any help would be greatly appreciated,
Regards,
Marc

 
 
 

Sorting a 2 column text file

Post by Ralf Fasse » Tue, 11 Mar 2003 04:06:08



| which are race times for various people. I want to sort by the time
| column, but only want the fastest time, i.e so that im left with

Check the -n option to the `sort' command, -r for reversing the sort
order.  Also see `head' or `tail' for truncating the number of lines.

R'

 
 
 

Sorting a 2 column text file

Post by Marc » Tue, 11 Mar 2003 04:10:59


$sort -nr [file]
15.00:john doe
12.32:john doe
11.70:adam a anaconda
11.56:zachariah zebedee
9.66:john doe
9.60:william wordsworth
9.10:william wordsworth

I could do that, but I only wanted the fastest time per person. Any further
suggestions?
Marc



> | which are race times for various people. I want to sort by the time
> | column, but only want the fastest time, i.e so that im left with

> Check the -n option to the `sort' command, -r for reversing the sort
> order.  Also see `head' or `tail' for truncating the number of lines.

> R'

 
 
 

Sorting a 2 column text file

Post by Ralf Fasse » Tue, 11 Mar 2003 04:25:43



| $sort -nr [file]
--<snip-snip>--
| I could do that, but I only wanted the fastest time per person. Any further
| suggestions?

Sorry, I answered too quick.  Maybe use awk/gawk for that?

  sort -nr file | awk -F: '{time[$2] = $0} END { for (x in time) {print time[x]}}'

R'

 
 
 

Sorting a 2 column text file

Post by Marc » Tue, 11 Mar 2003 05:05:55


Fantastic, many thanks, was really stuck there.

Regards,
Marc



> | $sort -nr [file]
> --<snip-snip>--
> | I could do that, but I only wanted the fastest time per person. Any
further
> | suggestions?

> Sorry, I answered too quick.  Maybe use awk/gawk for that?

>   sort -nr file | awk -F: '{time[$2] = $0} END { for (x in time) {print
time[x]}}'

> R'

 
 
 

Sorting a 2 column text file

Post by Bill Marcu » Tue, 11 Mar 2003 11:26:53


On Sun, 9 Mar 2003 19:10:59 -0000, Marc

> $sort -nr [file]
> 15.00:john doe
> 12.32:john doe
> 11.70:adam a anaconda
> 11.56:zachariah zebedee
> 9.66:john doe
> 9.60:william wordsworth
> 9.10:william wordsworth

> I could do that, but I only wanted the fastest time per person. Any further
> suggestions?
> Marc

sort -u

--
"Trusted Computing": the biggest oxymoron since "Military Intelligence"
or "Congressional Ethics"

 
 
 

Sorting a 2 column text file

Post by Barry Kimelma » Fri, 14 Mar 2003 23:31:14



werx.com) says...

Quote:> Have the following text file:

> 9.10:william wordsworth
> 9.60:william wordsworth
> 9.66:john doe
> 11.56:zachariah zebedee
> 11.70:adam a anaconda
> 12.32:john doe
> 15.00:john doe

> which are race times for various people. I want to sort by the time column,
> but only want the fastest time, i.e so that im left with

> 9.10:william wordsworth
> 9.66:john doe
> 11.56:zachariah zebedee
> 11.70:adam a anaconda

> Any help would be greatly appreciated,
> Regards,
> Marc

I am not sure if the "sort" command can handle this problem, but you CAN
write a simple Perl script to handle the problem :
(Note : you shpuld add the appropriate error checking)

$filename = $ARGV[0]; # get filename from command line

%race_times = ();
while ( $buffer = <INPUT> ) {
    chomp $buffer;

    $name = $fields[0];
    $time = $fields[1];
    if ( exists $race_times{$name} ) {
        if ( $time > $race_times{$name} ) {
            $race_times{$name} = $time;
        }
    }
    else {
        $race_times{$name} = $time;
    }

Quote:} # WHILE loop over file records

close INPUT;

while ( ($name,$time) = each %race_times ) {
    print $name,":",$time,"\n";

Quote:}

--
---------

Barry Kimelman
Winnipeg, Manitoba, Canada

 
 
 

Sorting a 2 column text file

Post by John W. Krah » Sat, 15 Mar 2003 14:28:57




> werx.com) says...
> > Have the following text file:

> > 9.10:william wordsworth
> > 9.60:william wordsworth
> > 9.66:john doe
> > 11.56:zachariah zebedee
> > 11.70:adam a anaconda
> > 12.32:john doe
> > 15.00:john doe

> > which are race times for various people. I want to sort by the time column,
> > but only want the fastest time, i.e so that im left with

> > 9.10:william wordsworth
> > 9.66:john doe
> > 11.56:zachariah zebedee
> > 11.70:adam a anaconda

> I am not sure if the "sort" command can handle this problem, but you CAN
> write a simple Perl script to handle the problem :
> (Note : you shpuld add the appropriate error checking)

> $filename = $ARGV[0]; # get filename from command line


Quote:> %race_times = ();
> while ( $buffer = <INPUT> ) {

You don't open the INPUT filehandle anywhere so this will fail.

>     chomp $buffer;

>     $name = $fields[0];
>     $time = $fields[1];

Oops, the time is in the first field and the name is in the second field.

Quote:>     if ( exists $race_times{$name} ) {
>         if ( $time > $race_times{$name} ) {
>             $race_times{$name} = $time;
>         }
>     }
>     else {
>         $race_times{$name} = $time;
>     }
> } # WHILE loop over file records
> close INPUT;

> while ( ($name,$time) = each %race_times ) {
>     print $name,":",$time,"\n";
> }

Oops, the OP wanted the output sorted by the time column.

#!/usr/bin/perl

while ( <> ) { # filename on command line
    my ( $time, $name ) = split /:/;
    $race_times{$name} = $time unless exists $race_times{$name};
    $race_times{$name} = $time if $race_times{$name} > $time;
    }

for ( sort { $race_times{$b} <=> $race_times{$a} } keys %race_times ) {
    print "$race_times{$_}:$_\n";
    }

__END__

John
--
use Perl;
program
fulfillment

 
 
 

1. SORT command > sorting decimal numbers in columns on a file not working !

Hi. I try to sort a column on a file containing decimal numbers. I use
Sse Linux 9.1. Then I try to do this:


doasidpoas   opipo     7.8
kjkljkljklji   kjklj   35.7
kljkljkl       kljkl   2.3
dopasidp       poipo   0.2
kjkljlkl       kjkl    0.0
kljkljkjl      ljlk    0.8
jklhkljkl      kjkl    0.0
kljkljkljl     kjkl    0.1

dopasidp       poipo   0.2
jklhkljkl      kjkl    0.0
kjkljlkl       kjkl    0.0
kljkljkjl      ljlk    0.8
kljkljkljl     kjkl    0.1
kljkljkl       kljkl   2.3
doasidpoas   opipo     7.8
kjkljkljklji   kjklj   35.7

kjkljkljklji   kjklj   35.7
doasidpoas   opipo     7.8
kljkljkl       kljkl   2.3
kljkljkljl     kjkl    0.1
kljkljkjl      ljlk    0.8
kjkljlkl       kjkl    0.0
jklhkljkl      kjkl    0.0
dopasidp       poipo   0.2

As you can see the sorting order is not correct. What am I doing wrong ?
I also tried to use '-g' option instead of '-n' but doesnt work.

Any idea ?

Thanks
Alberto

2. Linux Operating System

3. Sorting By Second Column With Unique First Column

4. Configure DNS on AIX

5. How to have sort start sorting froma given column?

6. Broke my lilo

7. how do I sort a file based on multiple columns

8. dhcp and multiple interfaces

9. Help to sort a file on particlular columns

10. How to sort variable columns file

11. Add a column of text in a text document

12. How To Transpose Row Into Column In A Text File ...

13. How to extract columns from a text file?