Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: No tools? Use Perl?!

by codiac (Beadle)
on Jul 28, 2016 at 10:42 UTC ( [id://1168717]=note: print w/replies, xml ) Need Help??


in reply to No tools? Use Perl?!

Your while loop looks at one line at a time but your text says "synopsis (multi line)" ... D'oh!

Even if you are disconnected from the net you must have some modules installed, maybe you can have a look around and see if someone else got a useful module installed.

If not, let's hope the files are small enough to fit in memory

# Suck in the whole file my $text = do { local $/; <$fh> }; # use a nested group to exclude the close tag while ($text=~ m{<ReportHost[^>]*>(?:(?!</ReportHost>).)*</ReportHost> +}s) { # print out the content of each ReportHost tag print "$1\n"; }

Untested! :)

Replies are listed 'Best First'.
Re^2: No tools? Use Perl?!
by haukex (Archbishop) on Jul 28, 2016 at 11:48 UTC

    Hi codiac,

    The regexp needs the /g switch or the code will loop forever, and you use $1 but don't have capturing groups.

    Regards,
    -- Hauke D

      The regexp needs the /g switch or the code will loop forever, and you use $1 but don't have capturing groups.
      I don't know the specifics as to how /g works. All I know is that in other scripts I've wrote it stops at the first instance. Considering that I have multiple <RemoteHost> sections I don't think it will work and thus am assuming that is what I want. I haven't set up $1 captures yet because it should only have one thing being feed to it; the file. I'll work on error handling for invalid input later.

        Hi Boyd.Ako,

        I don't know the specifics as to how /g works.

        /g on an m// match is documented in perlop, in this case: "In scalar context, each execution of m//g finds the next match, returning true if it matches, and false if there is no further match." Since the regexp is being used in a while loop, without the /g modifier it would simply match the first occurrence every time and the loop would never end. With /g, the match advances through the string.

        Alternatively: "The /g modifier specifies global pattern matching--that is, matching as many times as possible within the string. ... In list context, it returns a list of the substrings matched by any capturing parentheses in the regular expression." Without /g, it would only find the first match.

        When you add the /g and the capturing group to codiac's code it works:

        use warnings; use strict; my $text = <<'END'; <y><ReportHost>one</ReportHost> <x>test</x></y> <ReportHost> two </ReportHost> <ReportHost><z>thr</z>ee</ReportHost> END # m//g in scalar context while ($text=~ m{<ReportHost[^>]*>((?:(?!</ReportHost>).)*)</ReportHos +t>}sg) { print "a: \"$1\"\n"; } # m//g in list context my @m = $text=~ m{<ReportHost[^>]*>((?:(?!</ReportHost>).)*)</ReportHo +st>}sg; print "b: \"$_\"\n" for @m; __END__ a: "one" a: " two " a: "<z>thr</z>ee" b: "one" b: " two " b: "<z>thr</z>ee"

        Hope this helps,
        -- Hauke D

Re^2: No tools? Use Perl?!
by Anonymous Monk on Jul 28, 2016 at 11:40 UTC

    Your while loop looks at one line at a time but your text says "synopsis (multi line)" ... D'oh!
    So what is the problem? The tags he is matching do not contain a newline. The flip-flop maintains a state to govern the filter logic; there is no compelling reason to slurp the file.

Re^2: No tools? Use Perl?!
by Boyd.Ako (Novice) on Jul 29, 2016 at 01:58 UTC
    Your while loop looks at one line at a time but your text says "synopsis (multi line)" ... D'oh! What I mean is that the XML object data spans serveral lines between the opening and closing tags. It's not a <tag text="blah blah blah"></tag> but more of a
    <tag name="stuff"> blah blah blah </tag>
    Even if you are disconnected from the net you must have some modules installed, maybe you can have a look around and see if someone else got a useful module installed.
    From instmodsh:
    CPAN::Meta CPAN::Meta::Requirements CPAN::Meta::YAML Crypt::Blowfish_PP ExtUtils::CBuilder File::SearchPath IPC::Cmd JMX::JMX4Perl JSON::PP Locale::Maketext::Simple Module::Build Module::CoreList Module::Load Module::Load::Conditional Module::Metadata Params::Check Parse::CPAN::Meta Perl Perl::OSType Term::Clui Term::ShellUI Term::Size Test::Simple check_postgres parent version
    If not, let's hope the files are small enough to fit in memory
    I'm iffy to the concept of slurping and generally avoid it due to all the warnings that come with it. The files get anywhere from 3-5MB on a 2GB system with nagios running. Don't know if that's "small" enough.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168717]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found