Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort the content of a file in alphabetical order

Status
Not open for further replies.
Apr 30, 2003
56
US
I have a file load_bom.ctl and the content of the file as follows:

BMIV7.5-46-60K0,INV,BMIV7.5-46-60K0.dat
DMIV7.5-46-60K0,INV,DMIV7.5-46-60K0.dat
CMIV7.5-46-60K0,INV,CMIV7.5-46-60K0.dat
AMIV7.5-46-60K0,INV,AMIV7.5-46-60K0.dat
EMIV7.5-46-60K0,INV,EMIV7.5-46-60K0.dat

I need to write a perl script that reads this file and sort the file content in alphabetical order. How will I be able to achieve this? Please help. Thanks.
 
this might work:

Code:
open(FH,'<yourfile.txt') or die "$!";
my @lines = <FH>;
close(FH);
@lines = sort @lines;
print @lines;

how big is the file? If it's too big this might consume too much memory, but if it's a few megabytes or smaller you should be OK.
 
Does it have to be a perl script? I would have thought a command line option might have been faster in these circumstances, for a straightforward sort

--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Code:
open(INFILE,'<yourfile.txt') or die "$!";
open(OUTFILE,'>outfile.txt') or die "$!";

print OUTFILE sort <FH>;

close OUTFILE;
close INFILE;

Yes, it's that easy..
 
well it would be easier if I typed it in correctly...

the print line should be:
Code:
print OUTFILE sort <INFILE>;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top