I have to extract the id which is coming after YES..The output should be
VT: 001/89
VT: 003/56
i have written a script which is throwing up an error:
my $in_file = 'check.doc';
open my $in_fh, '<', $in_file or die "Could not open file $in_file: $!
+";
my $out_file = 'output.txt';
open my $out_fh, '>', $out_file or die "Could not open file $out_file:
+ $!";
while ( my $line = <$in_fh> )
{
chomp ($_);
$line if ($_ =~ /^Customer ID: Yes/);
print "$out_fh}";
}
close $in_fh or die "Could not close file $in_file: $!";
close $out_fh or die "Could not close file $out_file: $!";
I want to know the following :
currently im opening for a single file how can i do it for multiple files of the same extension?
Your error is due to the fact that you are reading the lines of input.txt into the $line variable, but you use the default variable $_ in the loop.
To add multiple files, simply loop through the file names, by wrapping the existing script with a for each construct, eg.
for my $filename (@ARGV){
To output the result in an Excel readable format you could either write it as a CSV file or if you are running Perl in a Windows environment you could use the Spreadsheet::WriteExcel module.
You asked the script to print out the file handle every time it encountered a valid record, so it did
Try this code, it takes a list of input file names as arguments on the command line.
#!/usr/bin/perl
use strict;
use warnings;
my $out_file = 'output.txt';
open (my $out_fh,'>',$out_file)||die "Couldn't open file $out_file:\n\t$!";
for my $in_file (@ARGV){
open(my $in_fh, '<', $in_file or die "Couldn't open file $in_file:\n\t $!";
while (<$in_fh>){
chomp;
if ($_ =~ /^Customer ID: Yes/){
my $line = readline($in_fh);
print $out_fh $line;
}
}
close $in_fh or die "Could not close file $in_file: $!";
}
close $out_fh or die "Could not close file $out_file: $!";
How exactly do you wish to present the data as a spreadsheet?