Posts Tagged ‘output’

PERL input and output files

Monday, November 17th, 2008

To read an input file, edit your data and put it to an output file try this:

#! /usr/bin/perl -w

use strict;

my $file = “in.csv”;
my $outfile = “out.csv”;

open(IN, “<$file”) or die “Could not open $file\n”;
open(OUT, “>$outfile”) or die “Could not open $outfile\n”;

while (<IN>)
{
chomp;
my @line = split(“,”); # this char is the separation sign.

# Your data will now be in a array called line[ ].

# Do whatever you want, access the data with the varable $line[FIELDNUMBER]
# Example: $line[3] = ‘blablbla’ #edit the 4th value from the input file.

print OUT “$line[0],$line[3],$line[2]“\n”; # write back to output file
}
# close the both files (importend! else the writing will not take place due caching!)
close(IN);
close(OUT);