in reply to Re: Searching a string in running file
in thread Searching a string in running file

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

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

    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

      Hi Hauke, The File::Tail can't be used because my server doesnt let us install anything , and this package is not installed on the machine, i was using the shell "tail" function but the problem with that is when the file ends this function gets hanged . Also since my requirement is to just see if that particular string gets repeated with different numbers , i can't figure out how to write a function for it because the log file is constantly be written by another script. could u give me a head start so that i can improvise and implement.

        With the shell tail function, you will hit the current EOF. You cannot read past the EOF. After awhile (suitable time delay for your application), you have to do another tail in order to see additional lines that have been added. This is normal.

        Without knowing much about your particular problem (one example line is not much to go on), sometimes in this situation, all that is needed is to monitor the size of the file and just verify that it continues to grow. my $size = -s "logfile";. Of course that won't work if the program continues to grow the file in spite of being in some kind of error mode.

        Update: The system tail command should be adequate for what you need. I presume that you are only doing this once per 30-60 seconds? Or maybe even less often? Also consider whether or not a simple line "eq" string comparison is adequate (current last line not identical to the last line at the last time that we checked). It could be that you don't even need a regex? Again without more details about this log file, it is impossible to say. If you show evidence to reject my simple "check file size" suggestion, then along the way you will provide us with enough data to write a proper regex for you.