in reply to Re: how to search for a string in a particular file?
in thread how to search for a string in a particular file?

#!/tools/packarch/public/perl/5.8.8/bin/perl use strict; use warnings; open(my $file, "<", "input.txt") or die "Can't open input.txt: $!" +; my $string = "icecream"; while (<$file>) { if (/$string/) { print "found string $string\n"; } else { print "did not find the string\n"; } }

this is what i could come up with. it worked correctly... but i'd like to know more about using the grep command.

Replies are listed 'Best First'.
Re^3: how to search for a string in a particular file?
by roboticus (Chancellor) on May 29, 2012 at 10:59 UTC

    tm86:

    When testing your code, you should give it some non-trivial test cases. For example, if you use this:

    Iow is the time for all good men to come to the aid of their party. I scream you scream, we all scream for ice cream. Or was it icecream?

    looks like it would give you the result:

    did not find the string did not find the string found string icecream

    Is that what you intended?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      #!/tools/packarch/public/perl/5.8.8/bin/perl use strict; use warnings; open(my $file, "<", "input.txt") or die "Can't open input.txt: $!"; my $string = "icecream"; while (<$file>) { if (/$string/) { print "found string $string\n"; } else { my $notfound = "not found"; } } if (my $notfound eq "not found") { print "string not found\n"; }

      I'm seeing this error now: Use of uninitialized value in string eq at ./testScript.pl line 19, <$file> line 8.

      What does this mean? the file input.txt does not contain the string "icecream" so ideally i should see the output at "string not found"

      ps. thank you guys for all your help!

        tm86:

        Each time you use "my" you declare a new variable. So at the line in question, you're declaring a new variable called $notfound, and then testing it to see if it contains "not found" before you ever set it to a value.

        Here's a quickie to show you what I mean:

        #!/usr/bin/perl use warnings; my $s='the quick red fox'; print $s, "\n"; my $s='Foo'; print $s, "\n"; { my $s="Bar"; print $s, "\n"; my $s; print $s, "\n"; } print $s, "\n";

        When I run it, I get:

        $ perl t.pl "my" variable $s masks earlier declaration in same scope at t.pl line +5. "my" variable $s masks earlier declaration in same scope at t.pl line +10. the quick red fox Foo Bar Use of uninitialized value $s in print at t.pl line 11. Foo

        There are several things to notice here:

        • If you use warnings, perl will tell you when you declare a new variable that has the same name as one you already have (the first two lines of output from my script).
        • When you start a new block, you can create a new version of a variable without perl giving you an error message (only two warnings for the four declarations).
        • When a block goes out of scope, then the previously-hidden (masked) variable is available again. (The "Foo" at the end.)
        • Immediately after creating a variable, it is undefined (the "use of uninitialized value" warning).

        So you're close, but you need a couple tweaks:

        #!/tools/packarch/public/perl/5.8.8/bin/perl use strict; use warnings; open(my $file, "<", "input.txt") or die "Can't open input.txt: $!"; my $string = "icecream"; my $notfound = ""; while (<$file>) { if (/$string/) { print "found string $string\n"; } else { $notfound = "not found"; } } if ($notfound eq "not found") { print "string not found\n"; }

        As you can see, I put a declaration at the top and initialized it to "". Then removed all the extra declarations.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re^3: how to search for a string in a particular file?
by ww (Archbishop) on May 29, 2012 at 10:33 UTC
    For more on perl's grep try typing
    perldoc -f grep
    at your command prompt.

    For more on your system grep... well, you'll have to consult man or some other system help.

    Update below.

    Your code is coming along nicely, but the highly verbose output may be (for a guess) why you want to consider grep.

    As written, it will spit out the 'not found' message for each line lacking "icecream" as well as correctly acknowledging those lines where the search pattern exists. There are a couple ways around this.

    • slurp (use search here) the entire file, if it's not too big for available RAM; concatenate the lines and search them
    • Omit the else (considered bad form in an if clause by some... and won't tell you when the file contains no instances of icecream, unless you add some additional code -- say, making the else clause set a flag which you read only after the entire file has been scanned.

    There are, of course, many other ways to do this, but exploring those ideas should be useful in the long run.

Re^3: how to search for a string in a particular file?
by vinian (Beadle) on May 29, 2012 at 14:20 UTC

    grep "icecream" input.txt it only output the line that contain the "icecream". you can get more information by type man grep or grep --help.

Re^3: how to search for a string in a particular file?
by Anonymous Monk on Jul 30, 2019 at 07:08 UTC
    I used the above code it worked for me , can you please help me to find multiple strings in a single file.