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:
Perl find and replace one liner

 



tbone587
User

Mar 17, 2011, 12:42 PM

Post #1 of 6 (1727 views)
Perl find and replace one liner Can't Post

I am using a perl find and replace one liner within a script, and am having a slight issue. I am taking input for a current name and new name. I then perform an exact match on the current name to see if the line exists in a file. If it exists I use a perl one liner to replace the current name with new name. The problem I am having is I cant get it to replace using an exact match on the current name. So if the following lines exist: Tristan, Tristan_2, and Tristan_3 and I try to replace Tristan with Tristan_1, it will rename the others to Tristan_1_2, Tristan_1_3. Any help would be greatly appreciated, code example is below:


Code
print "Enter current name: "; 
chomp($CurrentName=<STDIN>);

print "Enter new name: ";
chomp($NewName=<STDIN>);


open(FILE, "hosts.cfg") || die "Cannot open file: $!\n";

$found = 0;
foreach my $line(<FILE>)
{
chomp($line);
if($line =~ m/$CurrentName$/ )
{
$found = 1;
last;
}
}
if ( $found == 0 )
{
print "$CurrentName has not been found in system. Please rerun script \n";
exit;
}
else
{
print "$CurrentName has been found in system and updated.\n";
}


$ReplaceHosts = "perl -pi -e 's/$CurrentName/$NewName/g' hosts.cfg";
system $ReplaceHosts;

close (FILE);



BillKSmith
Veteran

Mar 17, 2011, 2:43 PM

Post #2 of 6 (1724 views)
Re: [tbone587] Perl find and replace one liner [In reply to] Can't Post

Your code would rename Trintan_2 and Tristan_3 even if there was no Tristan. Your first regular expression has to be able to find a name not just the string of characters. This of course depends on your data. Is a name always at the end of a line? Always surrounded by whitespace? The substitue command also has to be able to substitue only in a word and not change anything around it.
Good Luck,
Bill


tbone587
User

Mar 17, 2011, 3:06 PM

Post #3 of 6 (1721 views)
Re: [BillKSmith] Perl find and replace one liner [In reply to] Can't Post

Bill I understand that my code does what it does but I don't know how to fix. The name will not always be in the beginning of a line. How do I do a complete match on input and a complete match on the perl one liner?


BillKSmith
Veteran

Mar 17, 2011, 8:22 PM

Post #4 of 6 (1713 views)
Re: [tbone587] Perl find and replace one liner [In reply to] Can't Post

Sorry, I did not notice the "$" at the end of your first regular expression. It requires an exact match at the end of a line. Add a similar "$" to your second regular expression, and it will work the same way. Note: If the name occurs at the end of more than one line, it will change all of them. You will still have the same problem with longer names that end the same as the one you want. (e.g. Atristian would be changed to Atristian_01.) Unless your data is very special, you can fix that by adding a \b (word boundry) before the name in both regular expressions.
Good Luck,
Bill


tbone587
User

Mar 18, 2011, 4:59 AM

Post #5 of 6 (1704 views)
Re: [BillKSmith] Perl find and replace one liner [In reply to] Can't Post

Bill I have tried the following command, but it gives me the error "Substitution replacement not terminated at -e line 2.":


Code
$ReplaceHosts = "perl -pi -e 's/($CurrentName)$/$NewName/g' hosts.cfg"; 
system $ReplaceHosts;



BillKSmith
Veteran

Mar 18, 2011, 6:57 AM

Post #6 of 6 (1699 views)
Re: [tbone587] Perl find and replace one liner [In reply to] Can't Post

Some operating systems require double quotes with the -e. Remember to escape them with backslash because they are already inside a double quote. I found that the middle slash also has to be escaped. You do not need the parenthesis around "CurrentName".
Good Luck,
Bill

 
 


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

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