Loading ...
Sorry, an error occurred while loading the content.
 

Re: Using GREP to check if text string exists. If so...run code

Expand Messages
  • daymobrew@yahoo.com
    ... Charles answered this in post 20046. http://groups.yahoo.com/group/perl-beginner/message/20046 i.e. (match a line which has 0 AAA ) if ( /0 AAA / ) { The
    Message 1 of 2 , Nov 25, 2004
      --- In perl-beginner@yahoogroups.com, "markev_123" <markev_123@y...>
      wrote:
      >
      >
      > Hi,
      >
      > I have a perl script that does some text manupulation. I only want
      > the script to run on certain files based on a text string in the file.
      >
      > How can I use Perl's GREP to check a file for "FOUND TEXT" and then
      > if it exists, run my other code?
      >
      >
      > if ??? {
      > print 'match found';
      > RUN CODE...
      > } else {
      > print 'No match found';
      > DO NOTHING
      > }

      Charles answered this in post 20046.
      http://groups.yahoo.com/group/perl-beginner/message/20046
      i.e. (match a line which has '0 AAA ')
      if ( /0 AAA / ) {

      The above works if you don't read the line into a variable (using
      something like '<>' or <FH>').

      If you read the line into a variable, like $line, you have a slightly
      different 'if' statement.
      if ( open( FH, 'file.txt' ) )
      {
      my $line;
      while ( $line = <FH> )
      {
      if ( $line =~ /0 AAA / )
      {
      print "FOUND: $line";
      }
      }
      close( FH );
      }

      http://groups.yahoo.com/group/perl-beginner/message/20040
      I know you replied in post 20040 "That doesn't work. Syntax error."
      when I suggested the same syntax ($line =~ ) for an earlier problem
      you were having but I have tested the above code (5.6.1) and it works
      as expected i.e. it prints out lines with '0 AAA '.
    Your message has been successfully submitted and would be delivered to recipients shortly.