Appending line/lines with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending line/lines with sed
# 1  
Old 07-20-2005
Appending line/lines with sed

Hi folks,

I need to append line or bulk of lines into a file.
For example,I have the following section in opmn.xml file:
<process-type id="OC4J_RTEadmin_NIR" module-id="OC4J">
<module-data>
<category id="start-parameters">
<data id="java-options" value="-server -Djava.security.policy=/home/ias/v10.1.2/j2ee/OC4J_RTEadmin_NIR/config/java2.policy -Djava.awt.headless=true"/>
<data id="oc4j-options" value="-properties"/>
</category>
<category id="stop-parameters">
<data id="java-options" value="-Djava.security.policy=/home/ias/v10.1.2/j2ee/OC4J_RTEadmin_NIR/config/java2.policy -Djava.awt.headless=true"/>
</category>
</module-data>
<start timeout="900" retry="2"/>
<stop timeout="120"/>
<restart timeout="720" retry="2"/>
<port id="ajp" range="3301-3400"/>
<port id="rmi" range="3201-3300"/>
<port id="jms" range="3701-3800"/>
<process-set id="default_island" numprocs="1"/>
</process-type>

I need to do the following:
1. Locating the line number of the string : id="OC4J_RTEadmin_$SCHEMA_NAME" (no problem with me).
2. Under this string ,finding the line number of the string <port id="jms" range="3701-3800"/> (no problem with me).
3. After the line number from paragraph 2 ,inserting the following lines:
<environment>
<prop name=\"LD_LIBRARY_PATH\" value=\"${ORACLE_HOME}/lib\"/>
<prop name=\"SHLIB_PATH\" value=\"${ORACLE_HOME}/lib32\"/>
</environment>

Is it possible to do it with sed command?

Thanks in advance,
Nir
# 2  
Old 07-20-2005
It would be better if you could put the input within the code tags for better readability.

The way sed is used in this case is,

Code:
sed '/search_string/r FILE' inputfile

where FILE has the contents of the which needs to be inserted after you find, the search_string.

I am continuing on the solution provided the other day by r2007.

Code:
sed '/process-type id="'"$SCHEMA_NAME"'"/,/<\/process-type>/{ /<port id="jms".*/r ENV_FILE; }' inputfile

Not tested tho'.

ENV_FILE contains

Code:
<environment>
<prop name=\"LD_LIBRARY_PATH\" value=\"${ORACLE_HOME}/lib\"/>
<prop name=\"SHLIB_PATH\" value=\"${ORACLE_HOME}/lib32\"/>
</environment>

vino
# 3  
Old 07-20-2005
Hi vino,

Thanks a lot!
I'm at home now,so I'll test it tomorrow and let you know.

Bye for now,
Nir
# 4  
Old 07-21-2005
Hi vino,

I ran the following command:
Code:
sed '/process-type id="'"$SCHEMA_NAME"'"/,/<\/process-type>/{ /<port id="jms".*/r foo.lst; }' opmn.xml

and I got the following error:
sed: -e expression #1, char 0: Unmatched `{'

opmn.xml=inputfile
foo.lst=ENV_FILE

Thanks in advance,
Nir
# 5  
Old 07-21-2005
Code:
sed -e '/process-type id="'"$SCHEMA_NAME"'"/,/<\/process-type>/p' 
-e '/<port id="jms".*/r foo.lst' opmn.xml

Vino
# 6  
Old 07-21-2005
Hi vino,

Thanks again.
The command runs now but it appends the lines in the wrong place.

The section looks now:
<process-type id="OC4J_RTEadmin_NIR" module-id="OC4J">
<module-data>
<category id="start-parameters">
<data id="java-options" value="-server -Djava.security.policy=/home/ias/v10.1.2/j2ee/OC4J_RTEadmin_NIR/config/java2.policy -Djava.awt.headless=true"/>
<data id="oc4j-options" value="-properties"/>
</category>
<category id="stop-parameters">
<data id="java-options" value="-Djava.security.policy=/home/ias/v10.1.2/j2ee/OC4J_RTEadmin_NIR/config/java2.policy -Djava.awt.headless=true"/>
</category>
</module-data>
<start timeout="900" retry="2"/>
<stop timeout="120"/>
<restart timeout="720" retry="2"/>
<port id="ajp" range="3301-3400"/>
<port id="rmi" range="3201-3300"/>
<port id="jms" range="3701-3800"/>
<environment>
<variable id="LD_LIBRARY_PATH" value="/home/ias/v10.1.2/lib" append="true"/>
<variable id="SHLIB_PATH" value="/home/ias/v10.1.2/lib32" append="true"/>
</environment>
<process-set id="default_island" numprocs="1"/>
</process-type>

The bulk was inserted after jms line.
I wanted to insert the lines right after the string :
process-type id="OC4J_RTEadmin_$SCHEMA_NAME"

It should be as the following:

<process-type id="OC4J_RTEadmin_NIR" module-id="OC4J">
<environment>
<variable id="LD_LIBRARY_PATH" value="/home/ias/v10.1.2/lib" append="true"/>
<variable id="SHLIB_PATH" value="/home/ias/v10.1.2/lib32" append="true"/>
</environment>
<module-data>
<category id="start-parameters">
<data id="java-options" value="-server -Djava.security.policy=/home/ias/v10.1.2/j2ee/OC4J_RTEadmin_NIR/config/java2.policy -Djava.awt.headless=true"/>
<data id="oc4j-options" value="-properties"/>
</category>
....................

Thanks in advance,
Nir
# 7  
Old 07-21-2005
Request you again to always put your input code/sample with the code tags. Helps in reading better.

Vino
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed for appending a line before a pattern and after another patter.

Hi All, I'm appending a line before a pattern and after another pattern(;) but omitting the first pattern - same as if comes duplicates before the second pattern. Also, I'm adding a word before the first pattern - Here is my file select blah blah hello select blah blah ; select... (6 Replies)
Discussion started by: Mannu2525
6 Replies

2. Shell Programming and Scripting

[sed] Replace one line with two lines

Literally cannot get this one, guys. Single line replacement is simple, but I am not understanding the correct syntax for including a new line feed into the substitution part. Here's what I got. (Cannot use perl) #!/bin/sh set -f #Start Perms export HOME=/home/test_user # End Perms... (6 Replies)
Discussion started by: Parallax
6 Replies

3. Shell Programming and Scripting

SED to remove a line above and lines below.

:confused:Hi All, I need help on removing lines in a text file. Sample file : When there is a match ip for IPAddress in my `cat ip.out`, proceed delete line above until string "Comp" is found. Thank you very much. ---------- Post updated at 12:56 AM ---------- Previous update was... (4 Replies)
Discussion started by: chiewming
4 Replies

4. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

5. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

6. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

7. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

8. Shell Programming and Scripting

Sed - Appending a line with a variable on it

Hi, I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer... (4 Replies)
Discussion started by: eltinator
4 Replies

9. UNIX for Dummies Questions & Answers

SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day. I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file. I.e. ----Begin Original Datafile----- Condition = XXX Header Line 1 Header... (1 Reply)
Discussion started by: selkirk
1 Replies

10. Shell Programming and Scripting

Appending line with sed works on Linux but not on Solaris

Hi folks, Our application installation uses "sed" command to append string after specific line or after line number. Both cases work perfect on Linux but fail on Solaris. The OS versions are Solaris 9 and Linux Red Hat AS 3. i.g: Linux: ----- file foo.txt aaa bbb ccc ddd root#... (4 Replies)
Discussion started by: nir_s
4 Replies
Login or Register to Ask a Question