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).
| [reply] |
|
|
# 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";
}
| [reply] [d/l] |
|
|
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
| [reply] [d/l] |
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:
- Read the data from the file into a string variable
- 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. | [reply] |
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.
| [reply] [d/l] |
|
|
#!/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. | [reply] [d/l] |
|
|
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. | [reply] [d/l] [select] |
|
|
|
|
|
|
|
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.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
I used the above code it worked for me , can you please help me to find multiple strings in a single file.
| [reply] |
|
|
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
| [reply] |