The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to insert a string at the end of a file read ahjiefreak Shell Programming and Scripting 5 12-10-2007 08:38 PM
Insert text file at a certain line. insania Shell Programming and Scripting 4 07-31-2006 11:46 PM
how to insert line break + string in vi (search & replace ) umen Shell Programming and Scripting 1 06-08-2006 08:42 AM
insert a line in a file RishiPahuja Shell Programming and Scripting 7 06-22-2005 12:47 AM
Insert a line as the first line into a very huge file shriek UNIX for Advanced & Expert Users 3 03-08-2005 10:22 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-25-2005
Registered User
 

Join Date: Jun 2004
Posts: 146
Failed to insert string into file before line

Hi folks,

I have the following program:
Code:
#! /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:
Code:
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:
Code:
sed ${lineNum}i'\"'"${sourceString}"'"\' ${dataSource}
And the results are:
......
Code:
"<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
Reply With Quote
Forum Sponsor
  #2  
Old 07-25-2005
Registered User
 

Join Date: Jun 2005
Location: Bangalore , INDIA
Posts: 28
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.
Reply With Quote
  #3  
Old 07-25-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
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
Reply With Quote
  #4  
Old 07-25-2005
Registered User
 

Join Date: Jun 2004
Posts: 146
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 :
Code:
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
Reply With Quote
  #5  
Old 07-25-2005
r2007's Avatar
Registered User
 

Join Date: Jun 2005
Location: China
Posts: 73
try:
Code:
sed ${lineNum}i'\'${sourceString}'\' ${dataSource}
or
Code:
sed ${lineNum}i\\${sourceString}\\ ${dataSource}
Reply With Quote
  #6  
Old 07-25-2005
Registered User
 

Join Date: Jun 2004
Posts: 146
Hi r2007,

I failed in both of the cases.

In the first :
......
Code:
echo "sed ${lineNum}i'\'${sourceString}'\' ${dataSource}" > nir.ksh
/bin/ksh nir.ksh
Code:
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:
......
Code:
echo "sed ${lineNum}i\\${sourceString}\\ ${dataSource}" > nir.ksh
/bin/ksh nir.ksh
Code:
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
Reply With Quote
  #7  
Old 07-25-2005
r2007's Avatar
Registered User
 

Join Date: Jun 2005
Location: China
Posts: 73
i see, you want to put the code in another script and run it. try it[untested]
Code:
echo "sed '${lineNum}i\\${sourceString}\\' ${dataSource}" > nir.ksh
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0