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 and shell scripting languages 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 11:38 PM
Insert text file at a certain line. insania Shell Programming and Scripting 4 08-01-2006 03:46 AM
how to insert line break + string in vi (search & replace ) umen Shell Programming and Scripting 1 06-08-2006 12:42 PM
insert a line in a file RishiPahuja Shell Programming and Scripting 7 06-22-2005 04:47 AM
Insert a line as the first line into a very huge file shriek UNIX for Advanced & Expert Users 3 03-09-2005 01:22 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-25-2005
nir_s nir_s is offline
Registered User
  
 

Join Date: Jun 2004
Posts: 148
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
  #2 (permalink)  
Old 07-25-2005
amit_sapre amit_sapre is offline
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.
  #3 (permalink)  
Old 07-25-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
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
  #4 (permalink)  
Old 07-25-2005
nir_s nir_s is offline
Registered User
  
 

Join Date: Jun 2004
Posts: 148
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
  #5 (permalink)  
Old 07-25-2005
r2007's Avatar
r2007 r2007 is offline
Registered User
  
 

Join Date: Jun 2005
Location: China
Posts: 73
try:

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

or

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

  #6 (permalink)  
Old 07-25-2005
nir_s nir_s is offline
Registered User
  
 

Join Date: Jun 2004
Posts: 148
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0