Sponsored Content
Top Forums Shell Programming and Scripting Failed to insert string into file before line Post 79058 by nir_s on Monday 25th of July 2005 06:34:06 AM
Old 07-25-2005
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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies

2. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

3. Shell Programming and Scripting

KSH - search for string and insert line above

Hi,, Please excuse my lack of knowledge here. I think I am tring to do something fairly advanced yet am a bit of a beginner..... I want to use a korn shell (as is the norm with others in my company) to write a script to search a file for a string and insert some text above that line. Sudo... (5 Replies)
Discussion started by: HugoDarley
5 Replies

4. Shell Programming and Scripting

How to insert string at particular 4th line in text file

I just want to add a particular string in text file using shell script text file format 1 columns-10 2 text=89 3 no<> 4 5 test-9876 6 size=9 string need to insert in 4th line <switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'> After inserting the... (8 Replies)
Discussion started by: puneet.goel
8 Replies

5. Shell Programming and Scripting

how to insert a string as a first line of the file

Hi I need to update a number of existing files and insert #!/bin/ksh line a the first line of the file. Is there any awk or sed command which would help me to do that instead of doing it manually? Thanks a lot -A (10 Replies)
Discussion started by: aoussenko
10 Replies

6. UNIX for Dummies Questions & Answers

insert string at end of line if it found from list

Hi all, can some one help me out file 1 i have 06/01 3:14 d378299 06/01 8:10 d642036 06/01 10:51 d600441 06/01 10:52 d600441 06/01 11:11 d607339 06/01 11:49 d398706 something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706) d607339... (5 Replies)
Discussion started by: zozoo
5 Replies

7. Shell Programming and Scripting

Awk, if line after string does not match insert

I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah. Sample file: interface atm 1/0.190158... (3 Replies)
Discussion started by: numele
3 Replies

8. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

9. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies

10. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies
All times are GMT -4. The time now is 05:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy