On 9 April 2015 at 05:40, ayush kabra <ayush.kab...@gmail.com> wrote:

> I need little bit help. i need to sort a file according to its 3rd column
> and i need the output. can you please help me on this.
> Input file we can take anyone either .txt or .xls or .csv. Please look
> into this and help me.
>


If you need something in process in an existing perl program, then
something like:

----
open my $fh, '<', $filename or die "Can't open $filename, $!";
my @entries;
while( my $line = <$fh> ) {
     chomp $line;
     my ( @fields )  = split / /, $file;
     push @entries, [ $line, $fields[3] ];
}
close $fh or die "Something went wrong closing $filename, $!";
for my $entry ( sort { $a->[1] cmp $b->[1] } @entries ) {
      printf "%s\n", $entry->[0];
}

---

However, it should be said that this approach ( and the one using system
sort listed by another reponder ), will _NOT_ work for XLS ( A binary
format ) and will _NOT_ work for "general CSV", and will only work for CSV
that you have carefully generated under strict criteria.

Because CSV is *not* a line oriented format ( it just approximates one, but
you can embed newlines within a quoted field ), and CSV is *not* safe to
assume there are no field delimiters inside the field ( Because you can
stash a , inside a "" ).

Thus, a general solution for 3 seperate formats here will require specific
handling of each possible format independently.


-- 
Kent

*KENTNL* - https://metacpan.org/author/KENTNL

Reply via email to