Issue in Perl script to print next line

December 20, 2011 at 05:26:29
Specs: Windows 7
I got multiple of documents in which i have to extract a line coming after a ID..The scenario is

Code:
Customer ID: none
VT : 002/89

Customer ID: Yes
VT: 001/89

Customer ID: none
VT: 006/85

Customer ID: Yes
VT: 003/56
I have to extract the id which is coming after YES..The output should be


Code:
VT: 001/89
VT: 003/56
i have written a script which is throwing up an error:

Code:
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: $!";

pls rectify my mistakes


I want to know the following :
1. currently im opening for a single file how can i do it for multiple files of the same extension?

2.How to write the output to an excel file?


See More: Issue in Perl script to print next line

Report •


#1
December 20, 2011 at 08:51:21
First, I'm not a Window's perl programmer, so this was done on Linux. You had multiple syntax problems:

#!/usr/bin/perl
use strict;
use warnings;

my $in_file = 'check.doc';
open(IN_FH, '<', $in_file) or die "Could not open file $in_file: $!
+";

my $out_file = 'output.txt';
open(OUT_FH, '>', $out_file) or die "Could not open file $out_file:
+ $!";
#while ( my $line = <$in_fh> )
while ( <IN_FH> )
   {
#   chomp $_;
   if( $_ =~ /^Customer ID: Yes/ )
      {
      my $nextline=<IN_FH>; # get the next line
      print OUT_FH $nextline ;
      }
   }
close IN_FH or die "Could not close file $in_file: $!";
close OUT_FH or die "Could not close file $out_file: $!";

# end script

Check out this link for a wrapper perl program to handle multiple files:
http://www.computing.net/answers/pr...

I have no experience in creating Excel files with perl, but you have to use this module:

Spreadsheet::WriteExcel

If you don't have it, you can download it at cpan.org:

http://search.cpan.org/search?mode=...

There are examples at the above link.


Report •

#2
December 22, 2011 at 10:10:29
nails,

It's actually better to use lexical filehandles like the OP was using instead of the bareword filehandles. It's also better the use a named (lexical) var like the OP was using instead of the global $_ var.


#!/usr/bin/perl

use strict;
use warnings;

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> ) {
    if ( $line =~ /^Customer ID: Yes/ ) {
        my $nextline =  <$in_fh>;
        print $out_fh $nextline;
    }
}

close $in_fh or die "Could not close file $in_file: $!";
close $out_fh or die "Could not close file $out_file: $!";


Report •
Related Solutions


Ask Question