Re: find/replace in place
On 12/19/05, Tony Heal <email@example.com> wrote:
> I have a database name I want to replace inside of an xml file. I can do
> this in several step using sed, but I would like to do it in a single step
> using perl. This is what I have in sed and what does not work in perl.
> PERL (single line in bash script)
> echo -n "Please enter the name of the new database: "
> read syelledb
> dbchange=`cat /tmp/data-sources.xml|grep database|cut -d ">" -f2|cut -d "<"
> /usr/bin/perl -pi -w -e 's/$dbchange/$syelledb/'
> I am not using all perl as I can only do a very limited amount in that.
> (just started learning)
You aren't passing a file (or input stream) to perl, so it has no
input on which to run. If you want to edit $file inplace, you could
/usr/bin/perl -pi.bak -w -e 's/$dbchange/$syelledb/' $file
Note the option "-i.bak". This will create a backup of $file as
$file.bak, which is very useful if your script does something wrong.
In general, if something works in bash, sed, awk, or C, there's a good
chance it'll work in perl in essentially the same way. You might have
to add a few more "$"s, especially when defining variables.
Michael A. Marsh