Failed to insert string into file before line

Hi folks,

I have the following program:

#! /bin/ksh
set -xv
export grepDataSource="</data-sources>"
export DB_HOST=tornado
export SCHEMA_NAME=IAS10G
export dataSource=data-sources.xml
sourceString="<data-source class=\"oracle.jdbc.pool.OracleConnectionCacheImpl\" name=\"RiGHTvDS\" location=\"jdbc/RiGHTvociDS\" xa-location=\"jdbc/xa/RiGHTvXADS\" ejb-location=\"jdbc/RiGHTvDS\" connection-driver=\"oracle.jdbc.driver.OracleDriver\" username=\"${SCHEMA_NAME}\" password=\"${SCHEMA_PASS}\" url=\"jdbc:oracle:thin:@${DB_HOST}:1521:${ORACLE_SID}\" inactivity-timeout=\"30\" max-connections=\"20\" min-connections=\"20\"/>"
lineNum=`grep -n ${grepDataSource} ${dataSource} | awk -F: '{print $1}'`
sed ${lineNum}i'\${sourceString}\' ${dataSource}

In this program I need to insert the string $sourceString before line </data-sources>.

My problem is with the parameter $sourceString:

latform:/tmp> ./update_data_source.ksh   
export grepDataSource="</data-sources>"
+ export grepDataSource=</data-sources>
export DB_HOST=tornado
+ export DB_HOST=tornado
export SCHEMA_NAME=IAS10G
+ export SCHEMA_NAME=IAS10G
export dataSource=data-sources.xml
+ export dataSource=data-sources.xml
sourceString="<data-source class=\"oracle.jdbc.pool.OracleConnectionCacheImpl\" name=\"RiGHTvDS\" location=\"jdbc/RiGHTvociDS\" xa-location=\"jdbc/xa/RiGHTvXADS\" ejb-location=\"jdbc/RiGHTvDS\" connection-driver=\"oracle.jdbc.driver.OracleDriver\" username=\"${SCHEMA_NAME}\" password=\"${SCHEMA_PASS}\" url=\"jdbc:oracle:thin:@${DB_HOST}:1521:${ORACLE_SID}\" inactivity-timeout=\"30\" max-connections=\"20\" min-connections=\"20\"/>"
+ sourceString=<data-source class="oracle.jdbc.pool.OracleConnectionCacheImpl" name="RiGHTvDS" location="jdbc/RiGHTvociDS" xa-location="jdbc/xa/RiGHTvXADS" ejb-location="jdbc/RiGHTvDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="IAS10G" password="" url="jdbc:oracle:thin:@tornado:1521:DB10g" inactivity-timeout="30" max-connections="20" min-connections="20"/>
lineNum=`grep -n ${grepDataSource} ${dataSource} | awk -F: '{print $1}'`
+ grep -n </data-sources> data-sources.xml
+ awk -F: {print $1}
+ lineNum=30
sed ${lineNum}i'\${sourceString}\' ${dataSource}
+ sed 30i\${sourceString}\ data-sources.xml
<?xml version="1.0" standalone='yes'?>
<!DOCTYPE data-sources PUBLIC "Orion data-sources" "http://xmlns.oracle.com/ias/dtds/data-sources-9_04.dtd">

<data-sources>
        <!--
                An example/default DataSource that uses
                Oracle JDBC-driver to create the connections. 
                This tag creates all the needed kinds
                of data-sources, transactional, pooled and EJB-aware sources.
                The source generally used in application code is the "EJB"
                one - it provides transactional safety and connection
                pooling. Oracle thin driver could be used as well,
                like below.
                url="jdbc:oracle:thin:@host:port:sid"
        -->

        <data-source
                class="com.evermind.sql.DriverManagerDataSource"
                name="OracleDS"
                location="jdbc/OracleCoreDS"
                xa-location="jdbc/xa/OracleXADS"
                ejb-location="jdbc/OracleDS"
                connection-driver="oracle.jdbc.driver.OracleDriver"
                username="scott"
                password="tiger"
                url="jdbc:oracle:thin:@//localhost:1521/oracle.regress.rdbms.dev.us.oracle.com"
                inactivity-timeout="30"
        />

${sourceString}
</data-sources>

As you can see,The parameter ${sourceString} was not converted.

I tried to add quotation marks before the parameter in the sed command:

sed ${lineNum}i'\"'"${sourceString}"'"\' ${dataSource}

And the results are:
......

"<data-source class="oracle.jdbc.pool.OracleConnectionCacheImpl" name="RiGHTvDS" location="jdbc/RiGHTvociDS" xa-location="jdbc/xa/RiGHTvXADS" ejb-location="jdbc/RiGHTvDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="IAS10G" password="" url="jdbc:oracle:thin:@tornado:1521:DB10g" inactivity-timeout="30" max-connections="20" min-connections="20"/>"
</data-sources>

The problem in this case is that the sed added quotation marks (") in the beginning and in the end of the line.

Which marks and where should I put around the parameter in the sed command?

Thanks in advance,
Nir

Hi Nir,

Any argument passed to sed in single quotes is not interpreted by shell before processing.

The shell will pass the argument as it as to sed for processing.

A work around for this problem:-

echo "sed ${lineNum}i'\${sourceString}\' ${dataSource}" > some_file
/bin/ksh some_file

Hope this solves your problem. :cool:

I am not sure whether this will work.

But why dont you try to echo the whole string into sourceString using a here document.

Check out the forum search for here-document.

vino

Hi amit_sapre and vino,

Thanks for your replies.
I prefer amit_sapre suggestion - echo sed command into file and run it.
My problem is now with the echo command.
I cannot issue the command :

echo "sed ${lineNum}i'\${sourceString}\' ${dataSource}" > some_file

because of the back-slashes (\) and the inverted commas(').
I need the "magic formula" for this echo.

Thanks in above,
Nir

try:

sed ${lineNum}i'\'${sourceString}'\' ${dataSource}

or

sed ${lineNum}i\\${sourceString}\\ ${dataSource}

Hi r2007,

I failed in both of the cases.

In the first :
......

echo "sed ${lineNum}i'\'${sourceString}'\' ${dataSource}" > nir.ksh
/bin/ksh nir.ksh
platform:/tmp> ./update_data_source.ksh 
+ sourceString=<data-source class="oracle.jdbc.pool.OracleConnectionCacheImpl" name="RiGHTvDS" location="jdbc/RiGHTvociDS" xa-location="jdbc/xa/RiGHTvXADS" ejb-location="jdbc/RiGHTvDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="IAS10G" password="IAS10G" url="jdbc:oracle:thin:@tornado:1521:DB10g" inactivity-timeout="30" max-connections="20" min-connections="20"/>
+ grep -n </data-sources> data-sources.xml
+ awk -F: {print $1}
+ lineNum=30
+ echo sed 30i'\'<data-source class="oracle.jdbc.pool.OracleConnectionCacheImpl" name="RiGHTvDS" location="jdbc/RiGHTvociDS" xa-location="jdbc/xa/RiGHTvXADS" ejb-location="jdbc/RiGHTvDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="IAS10G" password="IAS10G" url="jdbc:oracle:thin:@tornado:1521:DB10g" inactivity-timeout="30" max-connections="20" min-connections="20"/>'\' data-sources.xml
+ > nir.ksh 
+ /bin/ksh nir.ksh
nir.ksh[1]: cannot open data-source: No such file or directory

The second:
......

echo "sed ${lineNum}i\\${sourceString}\\ ${dataSource}" > nir.ksh
/bin/ksh nir.ksh
platform:/tmp> ./update_data_source.ksh   
+ sourceString=<data-source class="oracle.jdbc.pool.OracleConnectionCacheImpl" name="RiGHTvDS" location="jdbc/RiGHTvociDS" xa-location="jdbc/xa/RiGHTvXADS" ejb-location="jdbc/RiGHTvDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="IAS10G" password="IAS10G" url="jdbc:oracle:thin:@tornado:1521:DB10g" inactivity-timeout="30" max-connections="20" min-connections="20"/>
+ grep -n </data-sources> data-sources.xml
+ awk -F: {print $1}
+ lineNum=30
+ echo sed 30i\<data-source class="oracle.jdbc.pool.OracleConnectionCacheImpl" name="RiGHTvDS" location="jdbc/RiGHTvociDS" xa-location="jdbc/xa/RiGHTvXADS" ejb-location="jdbc/RiGHTvDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="IAS10G" password="IAS10G" url="jdbc:oracle:thin:@tornado:1521:DB10g" inactivity-timeout="30" max-connections="20" min-connections="20"/>\ data-sources.xml
+ > nir.ksh 
+ /bin/ksh nir.ksh
sed: can't read class=oracle.jdbc.pool.OracleConnectionCacheImpl: No such file or directory
sed: can't read name=RiGHTvDS: No such file or directory
sed: can't read location=jdbc/RiGHTvociDS: No such file or directory
sed: can't read xa-location=jdbc/xa/RiGHTvXADS: No such file or directory
sed: can't read ejb-location=jdbc/RiGHTvDS: No such file or directory
sed: can't read connection-driver=oracle.jdbc.driver.OracleDriver: No such file or directory
sed: can't read username=IAS10G: No such file or directory
sed: can't read password=IAS10G: No such file or directory
sed: can't read url=jdbc:oracle:thin:@tornado:1521:DB10g: No such file or directory
sed: can't read inactivity-timeout=30: No such file or directory
sed: can't read max-connections=20: No such file or directory
sed: can't read min-connections=20/: No such file or directory

Any more suggestions?

Thanks in advance,
Nir

i see, you want to put the code in another script and run it. try it[untested]

echo "sed '${lineNum}i\\${sourceString}\\' ${dataSource}" > nir.ksh

Bingo!!

Thanks r2007!
It works perfectly!

Best regards,
Nir