Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
perl -lne '/regex/ and print' file_to_test
#!perl
use strict;
use warnings;
my $word = 'cat';
my $string = findit();
if ($string) {
print 'true';
}
else {
print 'false';
}
sub findit {
while (<DATA>) {
print "$_";
return(1) if index($_,$word) > -1;
}
}
__DATA__
here is a dog
here is a pig
a horse is here
a cat is here
a cow is here
here is a horse
here is a dog
here is a pig
a horse is here
a cat is here
true
#!/usr/bin/perl -w
open(FILE,"my_foo");
if (grep{/my_word/} <FILE>){
print "word found\n";
}else{
print "word not found\n";
}
close FILE;
if your word in the file is My_word and not my_word it will not match. grep is case sensitive.
@stuff=qw(one two three four);
print "@stuff";
@f=grep /Three/i,@stuff;
print "\n",@f;
I said sensitive not insensitive, but what i wanted to say was 'grep and the regex this way is case sensitive'.However, saying that grep is case insensitive is wrong information, so that needed correction.
#!/usr/bin/perl
@num=qw(axc ace def cab ghi);
@f=grep {$_="x $_" if /ab/} @num;
print @f;
perluserpengo said:I was a little upset, because the man asked how can he use grep to find a word in a file and everybody told him how to do it except of an understandable way of using grep.
phn737 said:what about other alternative without using perl's grep; i was doing some reading and a few commented that grep might slow down the task as it has to read entire file.