Need help in Perl Script for printing next line

I got multiple of documents in which i have to extract a line coming after a ID..The scenario is

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

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 :

  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?

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.

Even if im replacing $ with $line im getting GLOB(0x38acc) thousands of times ..:frowning: im not getting it stil :frowning:

You asked the script to print out the file handle every time it encountered a valid record, so it did :wink:

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?

$ perl -ne '(/^Customer/ && /Yes$/)&&($f=1);if($f==1 && !/^Customer/){print $_;$f = 0}' inputfile
VT: 001/89
VT: 003/56

Or if you'd like, a simple grep would look like this: (I tried this in GNU Bash)

grep -A1 'Customer ID: Yes' inputfile | grep -v Customer | grep -v '\-\-'

the script works fine for .txt files only but for .doc its not working at all :frowning:

.txt is not equal to .doc

doc is microsoft word document, which totally has the different format ( in file level )

so, you cant read the doc file simply using the perl open method.

if you still want to use the .doc file and read it, then check here

ActivePerl FAQ - Using OLE with Perl

Through Sed..

sed -n '/Yes$/{n;p}'  inputfile