![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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}
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>
I tried to add quotation marks before the parameter in the sed command: Code:
sed ${lineNum}i'\"'"${sourceString}"'"\' ${dataSource}
...... 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> Which marks and where should I put around the parameter in the sed command? Thanks in advance, Nir |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
I need the "magic formula" for this echo. Thanks in above, Nir |
|
#5
|
||||
|
||||
|
try:
Code:
sed ${lineNum}i'\'${sourceString}'\' ${dataSource}
Code:
sed ${lineNum}i\\${sourceString}\\ ${dataSource}
|
|
#6
|
|||
|
|||
|
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
...... 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
Thanks in advance, Nir |
|
#7
|
||||
|
||||
|
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
|
||||
| Google The UNIX and Linux Forums |