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

I'm trying to create my first perl script, and could use some help. I have a file containing one word per line, e.g: PEAR PINEAPPLE ORANGE I would like to search in the file for a specific word, and if it's found I will write it to a file. It must be an exact match, so "PEAR" should return "true" but "APPLE" should return "false"

Replies are listed 'Best First'.
Re: Search for a string in a file
by ww (Archbishop) on Mar 01, 2010 at 15:22 UTC
Re: Search for a string in a file
by almut (Canon) on Mar 01, 2010 at 15:19 UTC
    $ perl -n -e 'print if $_ eq "PEAR\n"' infile > outfile

    or

    $ perl -n -e 'print if /^PEAR$/' infile > outfile

    The part in single quotes (the argument to the -e option) is the Perl script.  The -n option is equivalent to putting while (<>) { ... } around the code.

    $_ eq "PEAR\n" or /^PEAR$/ tests if the current line (the default variable $_ here) holds your search string, where $_ =~ /.../ is implicitly assumed in the second case, i.e. a regular expression is by default matched against $_.

Re: Search for a string in a file
by ikegami (Patriarch) on Mar 01, 2010 at 14:59 UTC
    Programs of the file → processing → file can easily been written using <>
    #!/usr/bin/perl use strict; use warnings; while (<>) { chomp; ...; print ...; }

    The program would be used as follows:

    perl script.pl infile > outfile

    The line is present in variable $_. Check it and skip the print when it's not the value you want.

Re: Search for a string in a file
by planetscape (Chancellor) on Mar 01, 2010 at 19:08 UTC
Re: Search for a string in a file
by Fletch (Bishop) on Mar 01, 2010 at 14:50 UTC
    word = ARGV.shift File.open( ARGV.shift, 'r' ) do |in| File.open( ARGV.shift, 'w' ) do |out| in.each do |line| if line.chomp! == word out.puts line end end end end

    Just do that in Perl and Bob's your paternal male sibling.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      #!/usr/bin/perl -l my $word = shift; open my $in, '<', shift or die $!; open my $out, '<', shift or die $!; while (<$in>) { print $out $_ if $_ eq $word; } close $in; close $out;

      Howdy, Uncle Bob! Nice error checking!


      print map{substr'hark, suPerJacent other l',$_,1}(11,7,6,16,5,1,15,18..23,8..10,24,17,0,12,13,3,14,2,4);

        Ruby's File.open throws an exception which the default runtime behavior handles pretty well on its own (not to mention the error message includes the filename which yours doesn't), so yes.

        $ ruby -e 'File.open( ARGV.shift, "r" ) { |f| puts f.readlines }' nott +here -e:1:in `initialize': No such file or directory - notthere (Errno::ENO +ENT) from -e:1:in `open' from -e:1

        And if you're going to be that pedantic you might want to check for the error from print when you try and print to a filehandle you've opened for reading . . .

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.