t-rex has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have to find a string from a txt file which is constantly getting written by some other script ( basically script A logs its result in log.txt file which runs for hours) , now i have to read the last line of the log.txt file ( by tail or something) and report to some other script that if the run is taking place in proper manner or not. the log.txt has one pattern

TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rulefilecycl +e 0

the numbers above keeps on changing but the string pattern remains constant pls help as i am not understanding how to implement this

Replies are listed 'Best First'.
Re: Searching a string in running file
by haukex (Archbishop) on Jul 18, 2016 at 11:29 UTC

    Hi t-rex,

    If monitoring "script A" is only possible via its logfile, you could use File::Tail and monitor the file, using a regex in a similar way you would when normally reading a file (perlintro).

    Now as for what the regex should look like and "report to some other script that if the run is taking place", your question doesn't contain enough information to give you a suggestion yet.

    Hope this helps,
    -- Hauke D

      Hello Hauke, thanks for the prompt suggestion. i will look into using the File::tail ( i read it for the first time though) basically i want to know if the script is reading fine the below string will keep repeating with diffeent/random numbers, so in that case how should be my regex to detect that

      TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rulefilecycl

      thanks

        Hi t-rex,

        A single example input is not a good basis for making a regular expression, you'll need to show several examples of strings that should match and several that shouldn't, and also describe what specific information in the string you're interested in. For example, the regexes /TPSM/ and /rulefilecycl/ both match the string you provided, but I have no idea if those will also match lines you don't want to have matched.

        Also, PerlMonks is more of a place to learn, so I recommend you have a look at perlretut and try writing a regex, and feel free to ask for help here if you run into trouble (How do I post a question effectively?). The following website is also very helpful in designing a regex, here's a link to get you started: https://regex101.com/r/oX5sC6 (note that not all of Perl's features are supported, but for simple regexes like in this case it's compatible).

        Hope this helps,
        -- Hauke D

Re: Searching a string in running file
by AnomalousMonk (Archbishop) on Jul 18, 2016 at 13:10 UTC

    If

    • You can figure out how to reliably tail a file with one process that's being (asynchronously?) written by another process;
    • The  'TPSM seed' field really is a hex number as  '234B8177' from your example data suggests (note the 'B' in the middle);
    • All the other numbers are decimal numbers;
    • All the non-numeric fields are constant text;
    • Variable whitespace is acceptable between fields
    Then this extraction code may be useful:
    c:\@Work\Perl\monks>perl -wMstrict -le "my @lines = ( qq{TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rulefi +lecycle 0\n}, qq{TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rilefu +llcycle 0\n}, ); ;; my $d_num = qr{ \d+ }xms; my $h_num = qr{ [[:xdigit:]]+ }xms; ;; for my $line (@lines) { print qq{[[$line]]}; ;; my $match = my ($seed, $p1, $p2, $p3, $err, $pid, $rfc) = $line =~ +m{ \A TPSM \s+ seed \s+ ($h_num) \s+ pass \s+ ($d_num) \s+ x \s+ ($d_num) \s+ x \s+ ($d_num) \s+ errors \s+ ($d_num) \s+ pid \s+ ($d_num) \s+ rulefilecycle \s+ ($d_num) \s* \Z }xms; ;; if ($match) { print qq{line ok, seed $seed p1 $p1 p2 $p2 p3 $p3 err $err +pid $pid rfc $rfc \n}; } else { print qq{line malformed}; } } " [[TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rulefilecy +cle 0 ]] line ok, seed 234B8177 p1 1 p2 2 p3 50 err 0 pid 34562 rfc 0 [[TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rilefullcy +cle 0 ]] line malformed

    I understand that this code is perhaps a bit advanced for you right now, but this is a learning experience. Please consider carefully the wise advice of haukex, and also see perlre and also some of the Pattern Matching, Regular Expressions, and Parsing tutorials in the PerlMonks Tutorials section.


    Give a man a fish:  <%-{-{-{-<