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";
}[/CODE]
[B]If I use undef $/ or $/="" means Can't get Line no/Col no of the Error. This is my actual Problem. [/B]
It's not working. It matches upto <h><\/h>\n only. Please suggest how to proceed.
Thanks in Advance.
vishwa Ram.
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