Need help in Perl Script for printing next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in Perl Script for printing next line
# 1  
Old 12-20-2011
Data 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

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

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?

Last edited by rajkrishna89; 12-20-2011 at 09:24 AM.. Reason: Additional info
# 2  
Old 12-20-2011
Quote:
Originally Posted by rajkrishna89
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: $!";

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.
Code:
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.
# 3  
Old 12-20-2011
Even if im replacing $ with $line im getting GLOB(0x38acc) thousands of times ..Smilie im not getting it stil Smilie
# 4  
Old 12-20-2011
You asked the script to print out the file handle every time it encountered a valid record, so it did Smilie

Try this code, it takes a list of input file names as arguments on the command line.

Code:
#!/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?

Last edited by Skrynesaver; 12-20-2011 at 10:16 AM..
# 5  
Old 12-20-2011
Code:
$ 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)
Code:
grep -A1 'Customer ID: Yes' inputfile | grep -v Customer | grep -v '\-\-'


Last edited by balajesuri; 12-20-2011 at 09:26 PM..
# 6  
Old 12-21-2011
.doc perl script

the script works fine for .txt files only but for .doc its not working at all Smilie
# 7  
Old 12-21-2011
.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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing line in shell script

Need assistance in getting a shell program . I have csv file and each line has comma separated number. I wanted to take of the comas and print each number in each line . below example. Appreicate your help Row with number ... (14 Replies)
Discussion started by: ajayram_arya
14 Replies

2. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

3. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

4. Shell Programming and Scripting

Help with printing new line in shell script

I'm having a script which outputs four different attributes A, B, C, D for a list of users. I want to insert expression such that once the output is generated for first user, output for next user should be printed in new line. Please help. (4 Replies)
Discussion started by: surdileep
4 Replies

5. Shell Programming and Scripting

Printing the line number in bash script

Hi, I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened. Thanks, (6 Replies)
Discussion started by: suryaemlinux
6 Replies

6. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

7. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

8. UNIX for Dummies Questions & Answers

Perl searching and printing multiple target in the same line

Hello, I'm trying to create a program in perl called myfind.pl; To use the program: (at the command line)$ program.pl keyword filename note: the keyword is any word or regular expression and it should display the result just like when you 'cat' the file name but with the keyword in... (2 Replies)
Discussion started by: Horizon666
2 Replies

9. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies

10. Shell Programming and Scripting

printing an empty line in a file (perl)

I know this must be really easy, but i can't get it to work I've got a perl script, with a file. I want to print an empty line, and the following doesn't seem to work: print nameoffile "\n" thanks for your help!! (3 Replies)
Discussion started by: kfad
3 Replies
Login or Register to Ask a Question