[KLUG Members] Parsing a large file

Dirk H Bartley members@kalamazoolinux.org
06 Feb 2004 15:38:01 -0500


On Fri, 2004-02-06 at 15:15, Andrew Eidson wrote:
> Well.. to lessen the burden it is a simple parse Column1, 2, 3 to file X
> then parse column1, 20,30 to file Y.. the program I am going to import these
> files into will not handle the file as a whole..
> 
> I guess I will have to see about a baby sitter so I can come this tuesday
> (who knows maybe I will get an extention on when it is suppose to be done)
> Though Perl maybe over my head since Python is the first language I am
> currently learning so I am a complete newbie to programming at all.
> 
This should get you started

#!/usr/bin/perl
$, = '  ';    # set output field separator for print this is a tab here
$\ = "\n";    # set output record separator for print
open( INFILE, "/home/user/inputfile" );
open( OUTFILE, "/home/user/outputfile" );

while (<INFILE>) {
  # split with a : as the delimiter or choose your own regex
  @Fld = split(":", $_, 99999);
  chomp $_; # remove any cr and lf
            # $_ is each line one by one
  #perform any sed type regex subs
  $Fld[2] =~ s/abc/xyz/g;
  # output in any order desired with the delimiter set above in $, $\
  print $Fld[23],$Fld[2],$Fld[17];
}