?
Solved

calling perl to do search/replace from a bash script

Posted on 2008-10-28
9
Medium Priority
?
512 Views
Last Modified: 2013-12-26
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>ifAdminStatus\.1<\/description>/<description>ETech<\/description>/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=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

Open in new window

0
Comment
Question by:Jm2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22826080
TOREPLACEFNL="s/<description>"$TOREPLACE"\."$COUNTER"<\/description>/<description>"$NEWDESC"<\/description>/g"
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22826135
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

0
 

Author Comment

by:Jm2005
ID: 22826142
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?
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 

Author Comment

by:Jm2005
ID: 22826171
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.
0
 

Author Comment

by:Jm2005
ID: 22826184
on those lines which cause issues with the replace... i get the following error:

Substitution replacement not terminated at -e line 1.
0
 

Author Comment

by:Jm2005
ID: 22826202
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.

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 2000 total points
ID: 22826357
If I'm understanding what you're doing,  this is probably easier to write as
DESC=change
XML=go
NR=0
TOREPLACE="ifAdminStatus"
perl -i.bak -pe 'BEGIN{chomp(@n=<STDIN>)}s/(<description>)'$TOREPLACE'\.(\d+)(<\/description>)/$1$n[$2-1]$3/g' <$DESC $XML
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 2000 total points
ID: 22826377
TOREPLACEFNL="s/<description>"$TOREPLACE"\."$COUNTER"<\/description>/<description>$NEWDESC<\/description>/g"
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 22826438
perl -p -i -e "$TOREPLACEFNL" $XML
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question