Gossamer Forum
Home : General : Perl Programming :

sort file

Quote Reply
sort file
please help,


I'm a newbie so please be patient.

I have a file, say file.txt.

The contents of the file are:

z
x
h
w
a



how do i open this file and sort it, and output it into a different file, say sorted.txt?


The code I've tried doesn't work:

$file="file.txt";
open FILE, $file or die "cannt open $stuff for read:$!";

while(<FILE>){
@INFO = $_;
$out="sorted.txt";
open OUT, ">>$out" or die "cannot open $out for write :$!";
foreach $letter(@INFO){
$sorted=$sorted+$letter;
}
print "$sorted";
print OUT "$sorted";

}




please can someone tell me what i need to do?

what would the correct code be?

Any help greatly appreciated....
Quote Reply
Re: [0069] sort file In reply to
$file="file.txt";
open(FILE, $file) or die "cannt open $stuff for read:$!";
@INFO = $_;

close(FILE);

@sorted = sort @INFO;

open(OUT, ">sorted.txt") or die "Failed to open file\n";

foreach $i (@sorted)

{ print OUT "$i\n"; }

close(OUT);



Should work for you
Quote Reply
Re: [0069] sort file In reply to
It would be quicker from the command line.

Code:
cat file.txt | sort > newfile.txt