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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitution Operator Problems 1

Status
Not open for further replies.

dkeim

Programmer
Joined
May 18, 2007
Messages
6
Location
US
I am trying to do a global replacement of a value in a directory. The command I am using is

"perl -p -i -e 's|<old text>|<new text>|g' *"

This of course touches all files in the directory and I am getting errors on executable programs that it pass through. Is there a way to select just the ones that need changing and then do the perl command just on that one? Such as

grep "<old text>" * |exec perl -p -i -e 's|<old text>|<new text>|g' $1

or something like that.
 
You could use a BEGIN block to weed out all non-textual files:
Code:
$ perl -p -i -e 'BEGIN{@ARGV = grep -T, @ARGV} s|<old text>|<new text>|g' *
 
Super - it worked like a champ!!!!!!!!

[bigsmile]
 
I am devising a method to send two values to a script that will does your command.

script name: cngit

Code:
perl -p -i -e 'BEGIN{@ARGV = grep -T, @ARGV} s|$1|$2|g' *

But it doesn't seem to be working. Any ideas
 
$1 and $2 are empty in that script. What are you expecting them to hold?
 
The script that I created, 'chgit' would be used on the command line like this.

Code:
$ chgit leia anakin

leia is an example of the old phrase to be replaced.

anakin is an example of the new phrase to be used.
 
You're expecting the OS to interpolate into that literal string.
I'd imagine that you need to pass those parameters into the perl script...
Code:
perl -p -i -e 'BEGIN{($p1,$p2)=(shift,shift);@ARGV = grep -T, @ARGV} s|$p1|$p2|g' $1 $2 *[code]

This would work for single word substitutions.
 
Actually, my command did work after all. I was testing in one of my test directories and forgot that UNIX/Linux is case sensitive. The command '$ chgit leia anakin' could not work because the word was LEIA (uppercase).

So everything appears to be just fine.

Thanks again
 
chgit is just a one line script that runs your perl command except with input variables.

Thanks for your help, everything seems to be working.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top