[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: find/replace in place



On Mon, Dec 19, 2005 at 03:38:23PM -0500, Tony Heal 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.
}  
} SED
} #!/bin/bash
} 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 "<" -f1`
} sed s/$dbchange/$syelledb/ /tmp/data-sources.xml > /tmp/data-sources.xml.tmp
} mv /tmp/data-sources.xml /tmp/data-sources.xml.orig
} mv /tmp/data-sources.xml.tmp /tmp/data-sources.xml
[...]

Why stoop to Perl for something so simple and easy, especially when you
have already written the sed for it? After all, sed stands for Stream
Editor and you just want a non-stream version of it... i.e. ed. So your new
script is:

#!/bin/bash
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 "<" -f1`
ed -s /tmp/data-sources.xml <<EOF
1,$s/$dbchange/$syelledb/g
w
q
EOF

Note that I added a 'g' to the end of the substitution command.

} Tony Heal
--Greg



Reply to: