This question refers to
How to replace text using greedy approach in sed?
I have to match multiline data in file and need to replace them with some other text using perl.
cat file
<strong>ABC
</strong>
perl script: code.pl
#!/bin/perl
open(fh, $ARGV[0]) or die "could not open file\n";
while($input = <fh>)
{
if($input =~/<strong>(.*?)\n(\s)*<\/strong>/)
{
print($1,"\n");
}
}
close(fh);
perl code.pl file
Output: No output
How to solve above pblm.
Regards
This example uses the File::Slurp module to read in the entire file at once.
It then uses a regex with the
gandsmodifiers. Thesallows.*?to match newline characters. Thegmakes the search global. Global meaning it will find all matches in the given string. Without thegonly the first instance would be replaced. If you want your search to be case insensitive, you can use theiregex modifier.The
${1}is a back-reference to the match in parentheses.This example produces: