start searching a word from the particular record on the result of first occurence change the value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting start searching a word from the particular record on the result of first occurence change the value
# 1  
Old 08-22-2012
start searching a word from the particular record on the result of first occurence change the value

Hi,
I need a script to start searching a word from the particular record on the result of first occurence i need to change the value in that record.

I have a input file like this

Code:
<properties> 
<add key="DeliveryWithinDay" value="False" /> 
<add key="ABC" value="23:00:00 PM" /> 
<add key="DEF" value="(GMT-06:00) " /> 
<add key="CVB" value="90" /> 
<add key="XCB" value="2" /> 
<add key="Restarts" value="0" /> 
</properties> 
<properties> 
<add key="ContactAddress" value="Home" /> 
<add key="AttemptInterval" value="10" /> 
<add key="Restarts" value="1000" /> 
<add key="DEF" value="(GMT-06:00) " /> 
<add key="CVB" value="90" /> 
<add key="RestartInterval" value="600" /> 
</properties> 
<properties> 
<add key="DeliveryNextDay" value="False" /> 
<add key="ABC" value="23:00:00 PM" /> 
<add key="DEF" value="(GMT-06:00) " /> 
<add key="CVB" value="90" /> 
<add key="XCB" value="2" /> 
<add key="Restarts" value="50" /> 
<add key="XAB" value="2" /> 
</properties>


I would like to search a word "ContactAddress"(its unique value in a file) from here onwards i want to search a word "Restarts"(multiple occurences in a file) on the first occurence of result(<add key="Restarts" value="1000" /> ) in that i want to change the value 1000 to 500).

Expected output will be

Code:
<properties> 
<add key="DeliveryWithinDay" value="False" /> 
<add key="ABC" value="23:00:00 PM" /> 
<add key="DEF" value="(GMT-06:00) " /> 
<add key="CVB" value="90" /> 
<add key="XCB" value="2" /> 
<add key="Restarts" value="0" /> 
</properties> 
<properties> 
<add key="ContactAddress" value="Home" /> 
<add key="AttemptInterval" value="10" /> 
<add key="Restarts" value="500" /> 
<add key="DEF" value="(GMT-06:00) " /> 
<add key="CVB" value="90" /> 
<add key="RestartInterval" value="600" /> 
</properties> 
<properties> 
<add key="DeliveryNextDay" value="False" /> 
<add key="ABC" value="23:00:00 PM" /> 
<add key="DEF" value="(GMT-06:00) " /> 
<add key="CVB" value="90" /> 
<add key="XCB" value="2" /> 
<add key="Restarts" value="50" /> 
<add key="XAB" value="2" /> 
</properties>

Thanks in advance
# 2  
Old 08-22-2012
Code:
sed '/ContactAddress/,/Restarts/{
/Restarts/s/[0-9][0-9]*/500/
}' file

# 3  
Old 08-22-2012
elixir,
Thanks for your quick reply.

value column may be a string also. how can i achieve this.

And one more thing, the search value(1000 or some string) and replace value(500 or a string) should be pass at runtime

Last edited by NareshN; 08-22-2012 at 10:12 AM..
# 4  
Old 08-22-2012
Code:
string=1000
repl=500
sed '/ContactAddress/,/Restarts/{
/Restarts/s/value="'"$string"'"/value="'"$repl"'"/
}' file

# 5  
Old 08-22-2012
elixir,

Thank you so much its working.
Could you please explain the command for my understanding
Code:
sed '/ContactAddress/,/Restarts/{
/Restarts/s/[0-9][0-9]*/500/
}' file

Is it possible to replace from its original file without redirecting to temporary file and again moving to original file.
I want to execute this as a command and not through the script

Last edited by NareshN; 08-22-2012 at 10:35 AM..
# 6  
Old 08-22-2012
This expression is specifying a range of addresses to work upon and will select all blocks of lines containg "ContactAddress" in first line and "Restarts" in the last line. Since you mentioned that "ContactAddress" occurs only once in the file, only 1 such block is possible to be addressed by this expression.

In that selected block, in the last line (having the word "Restarts" which is the first occurrence of that word after "ContactAddress"), substitute a string of 1 or more digits with 500.

Check if your sed version supports in-place editing with the -i option.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete last occurence of word using Linux command?

Hi all, I am trying to delete last occurrence of word using sed command. for example. I have input like this on a.id1 = b.id1 and on a.id2 = b.id2 and on a.id3 = b.id3 and and I am expecting output like this on a.id1 = b.id1 and on a.id2 = b.id2 and on a.id3 = b.id3 I just need to... (11 Replies)
Discussion started by: nsk
11 Replies

2. Shell Programming and Scripting

regex - start with a word but ignore that word

Hi Guys. I guess I have a very basic query but stuck with it :( I have a file in which I want to extract particular content. The content is between standard format like : Verify stats A=0 B=12 C=34 TEST Failed Now I want to extract data between "Verify stats" & "TEST Failed" but do... (6 Replies)
Discussion started by: ratneshnagori
6 Replies

3. Shell Programming and Scripting

Change first occurence

I'm not getting the syntax correct to change a line only on the first occurrence: I've tried to change only the first match and I've tried to change the from the second match forward sed 's/<B>PT#/<tr><td class=\"pt1\" width=\"40%\"><B>pt#/1' $file > tmpfile.html sed ... (0 Replies)
Discussion started by: dba_frog
0 Replies

4. Shell Programming and Scripting

Perl : Search for next occurence of a word in an array

I have an array as follows: Space: ABC Name: def Age: 22 Type: new Name: fgh Age: 34 Type: old Space: XYZ Name: pqr Age: 44 Type: new : : How can I separate the array with elements starting from Space:ABC until Space: XYZ & put them in a different array & so on... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

5. UNIX for Dummies Questions & Answers

counting the occurence of a word

In a file I have to count a particular word. like i need apache how many times. I tried this $ tr "\011" "\012\012"<foo1 | tr -cd "" |sort\uniq -c but I got result like this 32 apache 18 dns 12 doctor Please sugest me (4 Replies)
Discussion started by: pranabrana
4 Replies

6. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies

7. UNIX for Dummies Questions & Answers

search& count for the occurence of a word

Greetings, I need to search and count all the occurences of a word in all the files in a directory. Any suggestions greatly appreciated. Thanks (1 Reply)
Discussion started by: skoppana
1 Replies

8. Shell Programming and Scripting

Delete all occurence of a word in one shot

i have a file called file1 cat file1 i am namish namish lives in India India and namish both are good. I want to delete all the occurences of namish in one shot,if i do it with sed i guess all the lines will be deleted containing the pattern.Suggest me any idea without AWK. Thanks... (6 Replies)
Discussion started by: namishtiwari
6 Replies

9. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies
Login or Register to Ask a Question