Geeks Talk

Prepare for your Next Interview




How to match multiline when reading input file line by line

This is a discussion on How to match multiline when reading input file line by line within the PERL forums, part of the Web Development category; Dear all, I am doing QC Tool for XML Document. I need to generate error log with error(including line#,Col#). How I am following is, Code: while($_ =~ m/&...


Go Back   Geeks Talk > Web Development > PERL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-31-2007
Junior Member
 
Join Date: Oct 2007
Location: Chennai
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
vishwaRam is on a distinguished road
Thumbs up How to match multiline when reading input file line by line

Dear all,

I am doing QC Tool for XML Document. I need to generate error log with error(including line#,Col#).

How I am following is,


Code:
while($_ =~ m/<h><\/h>\n<p>/msg)
{
       print "Err/line/Col";
}
If I use undef $/ or $/="" means Can't get Line no/Col no of the Error. This is my actual Problem.

It's not working. It matches upto <h><\/h>\n only. Please suggest how to proceed.

Thanks in Advance.
vishwa Ram.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-07-2008
Junior Member
 
Join Date: Oct 2007
Location: India
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
amitbhosale is on a distinguished road
Re: How to match multiline when reading input file line by line

hi,

the below attached program resolves your following issue.

<tb>
Het blijvend beeld der Hollandse Kunst|1
De Bijbel in Holland's Schilder-cultuur|25
</tb>
Some other text
Output file:

Some other text
<tb>
<r><ce>Het blijvend beeld der Hollandse Kunst</ce><ce>1</ce></r>
</tb>
Some other text
##########################################
#!/usr/bin/perl
use strict;
use IO::File;

print "\n File operations";

my $source_file="/home/myprog/oldfile";
my $destination_file="/home/myprog/newfile";

my $input = IO::File->new("< $source_file") # open file in read mode
or die "Couldn't open $source_file for reading: $!\n";
my $output = IO::File->new("+> $destination_file") # open file for write mode
or die "Couldn't open $destination_file for reading: $!\n";

my $line;

while (defined($line = $input->getline())) {
chomp($line);

if ( $line =~ /^<tb>/ ) # if $line contain <tb> then only execute following block of code
{
print $output "<tb> \n"; # write <tb> tag in output file
while ($line !~ /<\/tb>/) {

$line=$input->getline(); # get next line of

STDOUT->print($line) if ($line =~ /^\w*\|*/);
if ($line =~ s/\|/<\/ce><ce>/g) { # search string | and replace with <\ce><ce>
chomp($line);
print $output "<r><ce>$line</ce></r> \n" #final string present in between <tb>...</tb>
}
}
print $output "</tb>\n"; # write close </tb> tag in output file
}
else
{
print $output "$line \n" # write rest of the code as it is.
}
}

$input->close();
$output->close();
##########################
output:
oldfile ===>
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
<tb>
Het blijvend beeld der Hollandse Kunst|1
De Bijbel in Holland's Schilder-cultuur|25
</tb>
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

and
newfile ===>

HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
<tb>
<r><ce>Het blijvend beeld der Hollandse Kunst</ce><ce>1</ce></r>
<r><ce>De Bijbel in Holland's Schilder-cultuur</ce><ce>25</ce></r>
</tb>
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Last edited by amitbhosale : 02-07-2008 at 07:47 AM.
Reply With Quote
Reply

  Geeks Talk > Web Development > PERL


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Line to ground fault occurs on a transmission line ykbharat Electrical Engineering 2 07-29-2008 07:02 AM
Append the result of another shell script to the last line of a file swaroopa Unix/Linux 1 06-26-2007 04:29 PM
Option in Unix for line by line execution sarith Unix/Linux 1 05-28-2007 12:22 PM
Second line in a MsgBox Geek_Guest QTP 3 04-19-2007 12:32 PM
How to continue to next line timmy Unix/Linux 1 08-20-2006 12:16 AM


All times are GMT -4. The time now is 12:08 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved