First, let's create a test bed so we can tell if we got it right!

use strict; my $correctanswer = "11408"; my $regexp = qr/^([\d]+)*(hullBts_static)/; while (<DATA>) { if ( /$regexp/ ) { if ( $1 eq $correctanswer ) { print( "Congratulations, the regexp is correct!" ); exit( 1 ); } else { die( "Error, match was $1" ) } } } die( "Error, never matched" ); __DATA__ 11299 pts/4 00:00:00 su 11303 pts/4 00:00:00 bash 11403 tty2 00:00:00 bsc_static 11406 tty2 00:00:00 rtpTrau_static 11408 tty2 00:11:30 hullBts_static 11418 pts/4 00:00:00 tail 12157 pts/5 00:00:00 su

Well if I run this test code I get:

Error, never matched at /tmp/hullbts.pl line 17, <DATA> line 7.

Clearly something is wrong. I suggest taking a closer look at the regexp and what you're trying to match.

First you want the beginning of a line. Then as many digits in a row as possible. Then anything until we find "hullBts_static". With this in mind let's have another look at a regexp:

my $regexp = qr/ ^ # beginning of line (\d+) # as many digits as we can find .* # anything at all hullBts_static # literally, "hullBts_static" /x;

When substituting this new regexp we get the following output from the test script:

Congratulations, the regexp is correct!

Note that the /x at the end of the regular expression merely allows us to spread the expression over several lines. That regular expression looks, more compactly, like /^(\d+).*hullBts_static/.


In reply to Re: Parsing 'ps' output by monarch
in thread Parsing 'ps' output by vineet2004

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.