CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Find And Replace

 



rlinux57
New User

Dec 14, 2015, 12:10 PM

Post #1 of 7 (1898 views)
Find And Replace Can't Post

I want to find and replace the text using STDIN, but it won't works, kindly help me.


Code
use strict; 
#!/usr/bin/perl -w
use warnings;

our $line = 0;
my $file_name = "test.txt";

print "Please enter username:\n";
our $Firstname = <STDIN>;
chomp ($Firstname);

print "Please enter replacing username:\n";
our $Secondname = <STDIN>;
chomp ($Secondname);


open(my $fh, '>>',$file_name) || die "file could not open $! \n";
while ($line = <$fh>)
{
if( $line =~ "s/$Firstname/$Secondname/g")
{
print "$line\n";
}

# else {
# die qq(String doesn't replace at all\n);
# }
}
close($fh);



BillKSmith
Veteran

Dec 14, 2015, 1:36 PM

Post #2 of 7 (1894 views)
Re: [rlinux57] Find And Replace [In reply to] Can't Post

I understand that you want to replace a username in an existing text file. This is a bit more difficult than you think. Your code cannot even get started because it opens the file for appending and then tries to read it.

Although it is not exactly true, you should consider it impossible to make such a change in an existing file. You have to read the file. Make the change in memory. Write a new file. If everything is successful, you can rename the new file to replace the old one. Perl does provide some support for this process. Refer to option -i on the documentation page perldoc perlrun.
Good Luck,
Bill


rlinux57
New User

Dec 14, 2015, 1:48 PM

Post #3 of 7 (1893 views)
Re: [BillKSmith] Find And Replace [In reply to] Can't Post

Thanks for the response.

Let me tell you one thing, if i used this code predefined find and replace string it works but when i get from STDIN it won't work:


Code
while ($line = <$fh>)  
{
if( $line =~ s/ubuntu/redhat/g)
{
print "$line\n";
}

}
close($fh);



BillKSmith
Veteran

Dec 14, 2015, 3:05 PM

Post #4 of 7 (1882 views)
Re: [rlinux57] Find And Replace [In reply to] Can't Post

Did you forget the chomp?

Code
use strict; 
use warnings;
my $simulated_file = << "EOF";
123
foo ubuntu fum
abc
EOF


print "Enter Old Package name: ";
my $old_package_name = <>;
chomp $old_package_name; # Remove newline.
print "Enter New Package name: ";
my $new_package_name = <>;
chomp $new_package_name; # Remove newline.
open my $fh, '<', \$simulated_file or die "Cannot open simulated_file";
while (my $line = <$fh>)
{
if( $line =~ s/$old_package_name/$new_package_name/g)
{
print "$line\n";
}

}
close($fh);


Sample Run:

Code
Enter Old Package name: ubuntu 
Enter New Package name: redhat
foo redhat fum

Good Luck,
Bill


rlinux57
New User

Dec 15, 2015, 12:13 AM

Post #5 of 7 (1869 views)
Re: [BillKSmith] Find And Replace [In reply to] Can't Post

Thanks bill, But this script only replace the content that you have defined in script i.e

123
foo ubuntu fum * foo redhat fum
abc
EOF


I want to find and replace the text that is already in file.
Yes i have used chomp but it won't works.


Zhris
Enthusiast

Dec 15, 2015, 7:17 PM

Post #6 of 7 (1852 views)
Re: [rlinux57] Find And Replace [In reply to] Can't Post

Bill provided a self contained script you could test out of the box, with the intention that you would modify it to suit your needs.

A straight forward method of editing a file "in place" is to read from your input file, modify lines, write to a temporary output file, replace your input file with the temporary output file:


Code
use strict;  
use warnings;

my $filepath = 'test.txt';
my $filepath_temp = 'test.txt.temp';

print "Enter Old Package name: ";
my $old_package_name = <>;
chomp $old_package_name; # Remove newline.
print "Enter New Package name: ";
my $new_package_name = <>;
chomp $new_package_name; # Remove newline.

open my $fh, '<', $filepath or die "Cannot open filepath '$filepath': $!";
open my $fht, '>', $filepath_temp or die "Cannot open filepath_temp '$filepath_temp': $!";

while (my $line = <$fh>)
{
$line =~ s/$old_package_name/$new_package_name/g;
print $fht $line;
}

close $fht;
close $fh;

rename $filepath_temp, $filepath;


Though its much simpler just to use Perl intepreter directly:


Code
perl -pi -se 's!$old_package_name!$new_package_name!g' -- -old_package_name=ubuntu -new_package_name=redhat test.txt


Chris


rlinux57
New User

Dec 16, 2015, 7:42 AM

Post #7 of 7 (1830 views)
Re: [Zhris] Find And Replace [In reply to] Can't Post

I have do this with below script.
Thanks bill, thanks for your cooperation, i'll mold it as per my requirement.



Code
============================================================= 
#!/usr/bin/perl -w
use strict;
use warnings;
my $file_name = "test.txt";

print "Please enter username:\n";
our $Firstname = <STDIN>;
chomp ($Firstname);

print "Please enter replacing username:\n";
our $Secondname = <STDIN>;
chomp ($Secondname);

open(my $in, '<', $file_name) || die "cannot open $file_name for read $!";
#open(my $out, '>>', $file_name) || die "cannot open $file_name for write $!";
open(my $out, '>', "$file_name.tmp") || die "cannot open $file_name.tmp for write $!";

while(<$in>)
{
print if(s/$Firstname/$Secondname/g); #only one # at front of line
print $out $_ ;
}
print "Changed Successfully\n";

close ($in);
close ($out);

# save copy of original file
rename ($file_name,"$file_name.bak") || die "problem with rename $!";
# replace original with the modified version
rename ("$file_name.tmp", $file_name) || die "problem with rename $!";
=============================================================


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives