Perl program to read a file, search a string in it and print the line having that string.

———————————————————————–

———————————————————————–

use strict;
use warnings;

my $file = “1.txt”;
my $search_str = “bye”;

&search_string_in_file(file=>$file,str=>$search_str);

sub search_string_in_file {
my %args = @_;
my $file = $args{file};
my $search_str = $args{str};

my $FH;
open ($FH, ‘<‘,$file) or die “Couldn’t open file: $!”;

while (<$FH>)
{
if ($_ =~ /$search_str/i)
{
print “\n $_ “;
}
}
}

———————————————————————–

———————————————————————–

Advertisements

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: