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

Replace a complex strng 1

Status
Not open for further replies.

swaroop

Programmer
Feb 4, 2001
100
US
I have few files in which there is a string Ex:

<SCRIPT PURPOSE=&quot;Include&quot; SRC=&quot;/cafe/scripts/validate.js&quot;></SCRIPT>
<SCRIPT PURPOSE=&quot;Include&quot; SRC=&quot;/cafe/scripts/CAFECommonFunctions.js&quot; LANGUAGE=&quot;JAVASCRIPT&quot;></SCRIPT>
<SCRIPT PURPOSE=&quot;Include&quot; SRC=&quot;/cafe/scripts/CAFEMessages.js&quot; LANGUAGE=&quot;JAVASCRIPT&quot;></SCRIPT>

<SCRIPT LANGUAGE=&quot;JAVASCRIPT&quot;>
<!--
function validateFormFields( oForm )
{
alert(&quot;Get into validateFormFields &quot;);



The string is '&quot;/cafe/scripts/' I wanted to change the string to '&quot;../scripts/' in all the files. I have a code here it is not at all working.

#!/bin/ksh
FINDSTRING='/cafe/scripts/'
REPLACESTRING='../scripts/'
for fl in *.jsp; do
mv $fl $fl.old
sed 's/FINDSTRING/REPLACESTRING/g' $fl.old > $fl
#rm -f $fl.old
done



Please some one help me..

Thanks in Advance.

Swaroop.
 
Try:

[tt]#!/bin/ksh
FINDSTRING='/cafe/scripts/'
REPLACESTRING='../scripts/'
for fl in *.jsp; do
mv $fl $fl.old
sed &quot;s#${FINDSTRING}#${REPLACESTRING}#g&quot; $fl.old > $fl
#rm -f $fl.old
done[/tt]

There were a couple of problems.
[ul][li]To reference a Korn shell veriable you need to prefix the name with a $, or ideally, surround it in ${ and }.
[li]sed's &quot;s///&quot; command would have been confused by the forward slashes in your search strings, but you can use any character in as a delimiter, so I used &quot;#&quot;.
[li]Surrounding the sed command in single quotes prevents the Korn shell from expanding the values of the FINDSTRING and REPLACESTRING variables.[/ul]

Annihilannic.
 
It is excellent, thanks for your help I was working in really a complex way look at the code below, don't laugh at it..

#!/bin/ksh
for fl in *.jsp; do
mv $fl $fl.old
sed 's/\/cafe\/scripts\//..\/scripts\//g' $fl.old > $fl
done
# Remove files with OLD
rm -Rf *.old


Thankyou ones again...


Regards,

Swaroop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top