Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

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/<h><\/h>\n<p>/msg) { print ...

Go Back   Geeks Talk > Web Development > PERL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

PERL PERL Scripts - Tips, Tricks, Issues, Latest News, PERL Resource and PERL related topics can be discussed in this forum.

Reply

 

LinkBack Thread Tools Display Modes
  #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 08:47 AM.
Reply With Quote
Reply

  Geeks Talk > Web Development > PERL

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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 08:02 AM
Append the result of another shell script to the last line of a file swaroopa Unix/Linux 1 06-26-2007 05:29 PM
Option in Unix for line by line execution sarith Unix/Linux 1 05-28-2007 01:22 PM
Second line in a MsgBox Geek_Guest QTP 3 04-19-2007 01:32 PM
How to continue to next line timmy Unix/Linux 1 08-20-2006 01:16 AM


All times are GMT -4. The time now is 02:04 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved