substitute string according line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substitute string according line number
# 1  
Old 07-19-2005
substitute string according line number

Hi all,

I have an xml file which have several sections as the following:

<process-type id="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/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/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>

Each section start with <process-type id="NIR" module-id="OC4J">
($SCHEMA_NAME=NIR) and ends with </process-type>

I need to substitute the range ports in the lines:
<port id="rmi" range="3201-3300"/>
<port id="jms" range="3701-3800"/>

I cannot do it with sed globally because it will substitute all the appearances in the file.
I can find the first line by grepping: process-type id=\"$SCHEMA_NAME\"
After that going down 14 lines and 15 lines and substituting the ports.

The question is : does "sed" know to get line number and replace the string in the specific line?

Thanks in advance,
Nir
# 2  
Old 07-19-2005
You can sed for

port id="ajp" and port id="rmi"

and then on those lines, you can make your changes.

Code:
sed -n -e 's/\(.*"ajp".*\)[0-9][0-9]*\(-\)[0-9][0-9]*\(.*\)/\11025\28000\3/g' inputfile

I have changed the port range for "ajp" from 3301-3400 to 1025-8000.

Those appearing as red are the matched pattern in the regex. indicated as \(...\)

Another thing, which you will have to do is the escaping of the " in the regex.

Vino
# 3  
Old 07-19-2005
Hi vino,

Thanks for your reply but you missed someting.
The problem that the section :
<port id="ajp" range="3301-3400"/>
<port id="rmi" range="3201-3300"/>
<port id="jms" range="3701-3800"/>

apears a lot in the xml file.

I need to isolate first the requested section by finding 14 lines above the $SCHEMA_NAME and locate the line number.
e.g:
lineNum=`grep -n "process-type id=\"${SCHEMA_NAME}\"" opmn.xml`
RmiLineNum=lineNum+14 (The line port id="rmi" range="3201-3300 is permanently apears 14 lines later after the line : process-type id=\"${SCHEMA_NAME}\"

And finally,I want to take line $RmiLineNum and substituting there.

How to do it?

Thanks again.
Nir
# 4  
Old 07-19-2005
Code:
sed '/process-type id="'"$SCHEMA_NAME"'"/,/<\/process-type>/{ /<port id="rmi"/s/foo1/bar1/; /<port id="jms"/s/foo2/bar2/; }'

# 5  
Old 07-19-2005
Hey r2007,

Thanks a lot!
It works fantastic!!

Best regards,
Nir
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies

2. Shell Programming and Scripting

Substitute string with an index number

Objective is to substitute Jan with 01, Feb with 02 and so on. The month will be provided as input. I could construct below awk and it worked. echo Jun | \ awk 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",mon," ")}{ for (i=1;i<=12;i++){ if ($1==mon) printf("%02d\n",i)} }' ... (4 Replies)
Discussion started by: krishmaths
4 Replies

3. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

4. Shell Programming and Scripting

substitute a line in a file if line number is available

Hi guys, is there are way to substitute the content of certain line in the file by another entry if line number is available? For example, I have a variable A="HCMLPBBG" and a file MYFILE. I need to substitute entry on line 18168 of MYFILE with the value of the variable "A". Is there a way to... (1 Reply)
Discussion started by: aoussenko
1 Replies

5. Shell Programming and Scripting

Get line number when matches a string

If I have a file something like as shown below, ARM*187878*hjhj BAG*88778*jjjj COD*7777*kkkk BAG*87878*kjjhjk DEF*65656*89989*khjkk I need the line numbers to be added with a colon when it matches the string "BAG". Here in my case, I need something like ARM*187878*hjhj... (4 Replies)
Discussion started by: Muthuraj K
4 Replies

6. Shell Programming and Scripting

Display string at line number

I have a code here , which should display lines 6,10,14,18,35 of a text file #!/bin/ksh line=6 line=10 line=14 line=18 line=35 for i in 1 2 3 4 5 do val=`echo ${line}` act=`awk 'NR~/^($val)$/' db_CHECKOUT.txt` done; This code is not working. The purpose of the line below is... (3 Replies)
Discussion started by: njafri
3 Replies

7. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

8. Shell Programming and Scripting

Match String and get line number and filename

Hi All, I'm new to unix shell scripting.. Could someone guide me. I have to search a string in the entire directory, once the search string is matched, it should print the line number of the string that matches and also the line and along with it, it should print the file name. Thanks,... (5 Replies)
Discussion started by: thenz
5 Replies

9. Shell Programming and Scripting

To substitute a string in a line to another string

Suppose, d=ABC*.BGH.LKJ Now I want to replace 'DEFGHIJ' instead of '*.B' and store the value in d. Any Idea? Can we use sed here? The outout should be like this: d=ABCDEFGHIJGH.LKJ Please help.. (4 Replies)
Discussion started by: Niroj
4 Replies

10. Shell Programming and Scripting

grep the string with the line number

Dear Masters, Here i have some doubts can anyone clarify?. Is it possible to grep the lines by specifying the line numbers. I know the line number which i want to grep. example: grep 40th line filename grep 50th line filename Need ur comments. (4 Replies)
Discussion started by: salaathi
4 Replies
Login or Register to Ask a Question