in reply to how to search for a string in a particular file?

That sounds like a good approach. After opening the file, you need to read its contents into a variable before you can check if your search string appears in there.

perlintro should teach you enough Perl to get started, and has similar enough examples to what you want to do. If you have trouble with a specific step, feel free to ask (and include the code you have written, and the problem you are facing).

  • Comment on Re: how to search for a string in a particular file?

Replies are listed 'Best First'.
Re^2: how to search for a string in a particular file?
by MSPM5 (Monk) on May 29, 2012 at 13:25 UTC

    I did this so, but only works when file is in one line

    # file.txt # one two three four five six seven eight nine ten # zero #!/usr/bin/perl use warnings; open(FILE, "file.txt"); $_ = <FILE>; close(FILE); if ($_ =~ /nine/) { print "found\n" } else { print "not found\n"; }

      I guess instead of using $_, you can use something like

      my @lines = <FILE>; while (<FILE>) { if ($_ =~ /nine/) { print "found\n"; } else { print "not found\n"; } }
      i haven't tried it, but i think this is how it's done