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

One liner

by manav_gupta (Acolyte)
on Jun 02, 2009 at 15:05 UTC ( #767622=perlquestion: print w/replies, xml ) Need Help??

manav_gupta has asked for the wisdom of the Perl Monks concerning the following question:

I have a bunch of files in a folder with data like so:
+--------------------------------------------------------------------- +----------------------------+ | Total SE In Database + +--------------------------------------------------------------------- +----------------------------+ + Total SE Database + ----------------- + 44835 + +--------------------------------------------------------------------- +----------------------------+ | Total SE ON + +--------------------------------------------------------------------- +----------------------------+ + Total SE ON + ----------- + 39035 + +--------------------------------------------------------------------- +----------------------------+ | Total SE ON For SNMP + +--------------------------------------------------------------------- +----------------------------+
I need to get that number "39035", and I've been tinkering with a one-liner (though yeah, I'm a rusty-newbie), and the following doesn't work for me:
perl -ne 'if (m/^Total SE ON.*(\d+)/s) {print "$1\n"}' *
Any suggestions Gurus?

Replies are listed 'Best First'.
Re: One liner
by ikegami (Patriarch) on Jun 02, 2009 at 15:14 UTC

    You're processing a line at a time, but the string your are trying to match spans two lines.

    You could switch to paragraph mode.

    perl -nle'BEGIN { $/ = "" } print /^Total SE ON.*?(\d+)/s' *
      Hmm... I did try that... but at least that particular version didn't work for me... Time to look at the paragraph mode.
        It's cause your blank lines aren't actually blank!
        $ perl -ple's/^\s+//' * | perl -ne'BEGIN { $/ = "" } print /^Total SE + ON.*?(\d+)/s' 39035
Re: One liner
by Roy Johnson (Monsignor) on Jun 02, 2009 at 15:21 UTC
    You need to tell it to read the file all at once instead of line-by-line. Then you need to adjust your regex to do multiline matching (and not to be so greedy as to eat the numbers):
    perl -0777 -ne 'if (m/^Total SE ON.*?(\d+)/ms) {print "$1\n"}' *
    Update: as ikegami notes, paragraph mode would also be suitable. The shorthand for that is -00 (those are zeroes). See perlrun

    Caution: Contents may have been coded under pressure.
Re: One liner
by AnomalousMonk (Archbishop) on Jun 02, 2009 at 19:00 UTC
    Update: After going back and looking more closely at all the little red and black plus-signs in the OP (which I probably should have done in the first place), it seems the assertion in the first paragraph of my reply below is not true, so please disregard this reply entirely!

    As far as I can tell from the highly folded (not to mention possibly spindled and mutilated) example text given in the OP, the string  'Total SE ON' seems to occur on its own line, but is preceded by a bunch of spaces and followed by bunches of spaces, dashes and spaces before a bunch of digits occurs. (There is a previous line with  'Total SE ON' also, but this seems to be preceded by a  '|' (pipe) character and has no digits anywhere after it.)

    In other words, the line you want to match looks like this (but with fewer spaces and dashes):
    '   Total SE ON    ----    1234   '

    If this is the case, I do not see why the original regex would not work with line-by-line processing if only the requirement that  'Total ...' be anchored at the start of the line (the function of the  '^' (hat) regex metacharacter), i.e., that it have no characters before it, would be removed.

    Also, in the context of line-by-line processing, the  //s regex modifier, which switches on 'dot-matches-all' regex behavior, is pointless.

    Further, the regex  /Total SE ON\D+(\d+)/ would seem to more clearly express what you are trying to achieve (the  \D regex character class represents any character not a digit): it's always best to try to 'say what you mean' with regexen.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://767622]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2023-06-09 15:28 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (36 votes). Check out past polls.