Jm2005
asked on
calling perl to do search/replace from a bash script
i wrote a small bash script to do a mass search/replace using 2 files...
$DESC is the file with the labels to put in the destination file
$XML is the destination file (the file with the entries that need to be replaced therein)
i have it echoing the regex line to stdout for diagnostic purposes and it looks like this on the first item:
toreplaceinfal: 's/<description>ifAdminSta tus\.1<\/d escription >/<descrip tion>ETech <\/descrip tion>/g'
if i take the 's/<description> and prepend a perl -p -i -e on the ordinary shell, it works fine, but it doesnt work from within the script...
$DESC is the file with the labels to put in the destination file
$XML is the destination file (the file with the entries that need to be replaced therein)
i have it echoing the regex line to stdout for diagnostic purposes and it looks like this on the first item:
toreplaceinfal: 's/<description>ifAdminSta
if i take the 's/<description> and prepend a perl -p -i -e on the ordinary shell, it works fine, but it doesnt work from within the script...
DESC=change
XML=go
NR=0
TOREPLACE="ifAdminStatus"
# Grab all the descriptions, count them, then correspond fo the second file #
for i in `seq 1 1`;
do
NR=`expr $NR + 1`
NRP="$NR"p
sed -n "$NR"p $XML | grep "<description>" > /dev/null
COUNTER=`expr $COUNTER + 1`
NEWDESC=`sed -n "$COUNTER"p "$DESC"`
TOREPLACE1="$TOREPLACE"$COUNTER
TOREPLACEFNL="'s/<description>"$TOREPLACE"\."$COUNTER"<\/description>/<description>"$NEWDESC"<\/description>/g'"
echo "toreplace1: $TOREPLACE1"
echo "newdesc: $NEWDESC"
echo "XML: $XML"
echo "toreplaceinfal: $TOREPLACEFNL"
# read BUF1
perl -p -i -e $TOREPLACEFNL $XML
TOREPLACEFNL="s/<descripti on>"$TOREP LACE"\."$C OUNTER"<\/ descriptio n>/<descri ption>"$NE WDESC"<\/d escription >/g"
BTW, what's the logic of
for i in `seq 1 1`
when that's a long hand way of writing
for i in 1
for i in `seq 1 1`
when that's a long hand way of writing
for i in 1
ASKER
okay, that worked!
one last piece of this....
some of the lines are either blank, or have text which may screw the regex up (in $newdesc) how to fix?
one last piece of this....
some of the lines are either blank, or have text which may screw the regex up (in $newdesc) how to fix?
ASKER
tintin: the logic of that the loop needs to run multiple times, (428 in this instance) once i get the proc debugged, i'll adjust the loop accordingly.
ASKER
on those lines which cause issues with the replace... i get the following error:
Substitution replacement not terminated at -e line 1.
Substitution replacement not terminated at -e line 1.
ASKER
below is the syntax of the regex, and the error.
newdesc: Lake Placid Point to Point
XML: go
toreplaceinfal: s/<description>ifAdminStatus\.161<\/description>/<description>Connection to Lake Placid Point to Point<\/description>/g
Substitution replacement not terminated at -e line 1.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.