Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl -i.orig -pe 's/OLD/NEW/e' does not work

Status
Not open for further replies.

hot1

Programmer
Mar 11, 2002
4
US
Hi,
I am trying to switch all occurences of the word "OLD" to "NEW" using either of the following simple commands, but my FILE.TXT does not change. What am I doing wrong? Neither of these commands seem to work. They do create a FILE.TXT.orig though.

>perl -i.orig -pe 's/OLD/NEW/e' FILE.TXT
>perl -i.orig -pe 's{\bOLD\b}{NEW}' FILE.TXT

Thanks.
David
 
try like this:

perl -pi.orig -e 's/OLD/NEW/g' FILE.TXT
 
Nope, did not work. Thanks for your help though. Happy New Year to you all and may you grow very rich <smile>.
I got this line from Perl Cookbook. Here's what the CookBook says:
========================
% perl -pi.orig -e 's/DATE/localtime/e'
will be executed as:

while (<>) {
if ($ARGV ne $oldargv) { # are we at the next file?
rename($ARGV, $ARGV . '.orig');
open(ARGVOUT, ">$ARGV"); # plus error check
select(ARGVOUT);
$oldargv = $ARGV;
}
s/DATE/localtime/e;
}
continue{
print;
}
select (STDOUT); # restore default output
========================

Here are the contents of my file.txt:
0
1
1
645
868773201911.000000
0.000000
dho
1103845281
275
10
'OLD''Time Periods''Currencies'
11
Hello, test
 
works fine here, too. If you're in windows, remember you need to use "" instead of '' around your CLI one-liners.

c:\> perl -pi.orig -e "s/OLD/NEW/g" FILE.TXT

________________________________________
Andrew

I work for a gift card company!
 
Works for me (ActiveState Perl v5.8.0 on Win XP Pro).

Note that you definitely don't need the /e modifier in this case. /e means "eval right hand side of re as Perl code." It's needed in the example from PCB because localtime is the name of a builtin Perl function and is hence code. The string "NEW" is not code, so no /e needed.
 
Ahh!!! I had to use double quotes!! Thanks kind sirs!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top