m

 

$ less metest.pl
use strict;
use warnings;

my $find = “this”;
my $replace = “that”;
my $substr=’Here’;
my $file = ‘data.txt’;
open my $info, $file or die “Could not open $file: $!”;

while( my $line = <$info>) {
if (index($line, $substr) != -1) {
$line=~ s/$find/$replace/g;
print $line;
}
}

close $info;

 

 

$ less data.txt
Here it is the case-1 , this can be avoided.
Here it is the case-2 , this can be avoided
Here it is the case-3
it is the case-4:

 

 

oye

my $message = <<‘END_MESSAGE’;my $message = <<‘END_MESSAGE’;this is a message .Here it is the case-1 , This can be avoided.Here it is the case-2 , This can be avoided.it is the case-3 , This can be avoided.END_MESSAGE
if (index($string, $substring) != -1) {   print “‘$string’ contains ‘$substring’\n”;} print $message;

use strict;use warnings; my $substring=’this’;my $substring2=’that’;
my $filename = ‘data.txt’;if (open(my $fh, ‘<:encoding(UTF-8)’, $filename)) {  while (my $row = <$fh>) {    chomp $row; if (index($row, $substring) != -1) { my $newString = $old =~ s/$substring/$substring2/r;   print $newString;}  }} else {  warn “Could not open file ‘$filename’ $!”;}

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s