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

Hi! I'm trying to search for a particular string inside a file... What i think i should do is: 1. open the file 2. if the file contains the string i want, then print a message saying that the string exists in the file else, print a message saying that the string does not exist. 3. close the file Please help me know how to write code for this!
  • Comment on how to search for a string in a particular file?

Replies are listed 'Best First'.
Re: how to search for a string in a particular file?
by moritz (Cardinal) on May 29, 2012 at 07:02 UTC

    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).

      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

Re: how to search for a string in a particular file?
by bart (Canon) on May 29, 2012 at 08:03 UTC
    This sounds like a homework problem, so I won't help too much on the simple code. However, your step 2 is still a bit too vague, and large. You'll have to split it up some more into subtasks:
    1. Read the data from the file into a string variable
    2. Search in the variable for the particular string
    Depending on the size of the file, and of the type of string you search for, you may slurp it all in at once, or read it in chunks, for example one line at a time, and search in each line in turn. This won't work well if the string you search for contains a newline anywhere other than at its end... Nowadays, computers have a lot of memory, and even largish files (megabytes) are "small" compared to the amount of memory available. So reading it all in one chunk is nowadays a decent approach.

    Now, how can you search for a string? You can use a regex, or index. For simple literal strings, and not patterns, index is the easier approach.

Re: how to search for a string in a particular file?
by vinian (Beadle) on May 29, 2012 at 08:07 UTC
    maybe you can try the command grep if you use *nix system.
      #!/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.

        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.
Re: how to search for a string in a particular file?
by Anonymous Monk on May 29, 2012 at 07:07 UTC

    Please help me know how to write code for this!

    Enter some of those terms into PerlMonks search , find many threads on this