Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by vinian (Beadle)
on May 29, 2012 at 08:07 UTC ( [id://972948]=note: print w/replies, xml ) Need Help??


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

maybe you can try the command grep if you use *nix system.

Replies are listed 'Best First'.
Re^2: how to search for a string in a particular file?
by tm86 (Initiate) on May 29, 2012 at 10:07 UTC
    #!/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.

      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!

      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.

      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.

      I used the above code it worked for me , can you please help me to find multiple strings in a single file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://972948]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-18 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found