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

Hi I need to retrive a string from a file which come after a specific pattern. eg I need to retive the word which comes after TARGET = in a file thanks in adnavce Shashank
  • Comment on Get string for a word in a file after some specific pattern compare

Replies are listed 'Best First'.
Re: Get string for a word in a file after some specific pattern compare
by mirod (Canon) on Mar 27, 2001 at 19:02 UTC
    my $s="TARGET=toto"; my ($word)= ($s=~/TARGET\s*=\s*(\w+)/); print "word: $word\n";'

    But you should really read the doc (perldoc perlre) or books like the Camel if you know that little and want to use Perl.

(Ovid) Re: Get string for a word in a file after some specific pattern compare
by Ovid (Cardinal) on Mar 27, 2001 at 19:07 UTC
    That's pretty vague. Here's a vague response: perlre.

    What do you mean by string? Do you mean everything that follows "TARGET=" and isn't a space? The general regex looks like this:

    my ( $target ) = ( $string =~ /TARGET=\s*(\S+)/ );
    But that's still pretty vague and might not fit what you want. Show us a few input records and exactly what data you want to extract from the records and we can better help you.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Get string for a word in a file after some specific pattern compare
by Masem (Monsignor) on Mar 27, 2001 at 19:04 UTC
    Assuming that it's a word with no spaces, then...
    while (<FILE>) { last if /TARGET\s?=\s?(\S*)\s/; } my $value = $1 || "not found";
    In other words, look for the string of interest (with some possible white space padding), and placeholder the 'word' that follows it up to the next white space. We break the loop if that exists, and the word will be in $1 from the regex. Otherwise, this should be null, and you'll know it's not there.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      If $1 was set before the while loop and the while loop fails, $value will have garbage in it.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      And the ever-popular:
      my $word; { # Memory is cheap...for some applications local $/ = undef; open( FILE, "< $file" ) or die( "Ack: $!" ); my $whole_file = <FILE>; close FILE; # Assume whitespace delimiting ( $word ) = $whole_file =~ /TARGET\s*=\s*(\S+)/; } # $word is now set to our value or undefined

      This slurps the entire file into memory and so isn't generally a good idea, but a variant of this approach *is* useful for more complex multi-line patterns. Particularly when you can use $/ = '\n\n'; or similar instead of $/ = undef; This can make many parsing jobs easier.

      Basically, if you find yourself trying to keep state when parsing a multi-line expression, always consider changing the definition of '$/', it is a very powerful tool.

      For those unfamiliar with this, evaluating <FOO> in a scalar context reads one 'line' from the filehandle FOO.

      The definition of the end of a line is defined by the pattern held in the '$/' variable. You can set this to whatever you want to define your own line-endings.

      Note bene: Don't use this to hack around DOS/Unix line ending conventions (CRLF versus LF). If you have problems with this look at the 'binmode' function call.

      Hmm....am I far enough off-topic yet? Time to stop this...

      $1 will not contain the word from the regex, because the special regex variables are automatically localized to the enclosing blocks. As perlre explains it:
      $<digits> Contains the subpattern from the corresponding set of parentheses in the last pattern matched, not counting patterns matched in nested blocks that have been exited already. (Mnemonic: like \digits.) These variables are all read-only.
      Since the while block has been exited, by the time you check $1 it contains the value it held before the while block was executed.

      Try this instead:

      my $value = 'not found'; while (<FILE>) { $value = $1, last if /TARGET\s?=\s?(\S*)\s/; }