Avatar of sreeman78
sreeman78 asked on

Pattern match


        I want to grep the value presnt inside <sql type="cognos">  XXXXX</sql>
in the attached file there are 98 occurences .
I want to grep this value (XXXXX) and push it inside an array .So that that array would contain all the XXXXX .I am attaching the file <model.xml>
my code
my $infile = shift;
open INFILE, "<", $infile;

while ($in = <INFILE>) {
    #print if (/<dataSourceRef>.*<\/dataSourceRef>/);
($input) = $in =~ (/<sql type="cognos">(.*?)<\/sql>/);
push @array,$input;

close INFILE;
print scalar @array ;
print join ("\n","@array\n");


Avatar of undefined
Last Comment
Suhas .

8/22/2022 - Mon
Carl Bohman

Your match is not working because .* does not match across line breaks (as exist in your file).  If I were you, I would change (.*?) to ([^<]*) in order to match everything up to the end tag: </sql>.  This will match across line breaks.  The other option is to add the "s" modifier after the last slash as in:
($input) = $in =~ (/<sql type="cognos">(.*?)<\/sql>/s);

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Suhas .

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.