[SOLVED] Replace a string in nextline after searching a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] Replace a string in nextline after searching a pattern
# 1  
Old 09-12-2012
[SOLVED] Replace a string in nextline after searching a pattern

Hi,

I have a requirement where I need to replace a string in a line and this line will be identified by search criteria on previous line:
E.g.:
I have an xml file and Contents as below:
Code:
<Root>
        <NameValue>
            <name>Global/Text/Data</name>
            <value>This is valid data</value>
        </NameValue>
        <NameValueBoolean>
            <name>Global/Text/ID</name>
            <value>@%ID_Value</value>
        </NameValueBoolean>
        <NameValue>
            <name>Global/Text/Data</name>
            <value>This is not a valid data</value>
        </NameValue>
        <NameValuePassword>
            <name>Global/Text/ID</name>
            <value>@%ID_Value</value>
        </NameValuePassword>
        <NameValue>
            <name>Global/Text/Data</name>
            <value>This is SMS data</value>
        </NameValue>
        <NameValuePassword>
            <name>Global/Text/ID</name>
            <value>%&ID_Value</value>
        </NameValuePassword>
</Root>

Here, I have to seach for string contains ID in it and need to replace value in next line only if it has @%.

Can someone help in sorting this?

Thanks,
Krishna

Moderator's Comments:
Mod Comment edit by bakunin: Please use code tags next time for your code and data.

Last edited by bakunin; 09-12-2012 at 03:40 PM..
# 2  
Old 09-12-2012
please post your desired output.


not able to test below script. try

Code:
awk -v VM="New_value" -F "[<>]" '{if ($0 ~ /\/ID/) {s=1} else { if($0 ~ /@%/) {if(s=1){ sub(/@%ID_Value/,VM);s=0}}else{s=0}}}1' file


Last edited by pamu; 09-13-2012 at 03:47 AM..
This User Gave Thanks to pamu For This Post:
# 3  
Old 09-12-2012
Code:
 awk '/ID</{print;getline;if(/@%/){sub(/ID_Value/,"CHANGE HERE");}} !/ID</' input_file

Whatever you wanna change just change in the text "CHANGE HERE".
This User Gave Thanks to msabhi For This Post:
# 4  
Old 09-12-2012
Thanks for Quick responses
Please find the original and Desired Output details:

Code:
 
Original:
<Root>
<NameValueBoolean>
<name>Global/Text1/ID</name>
<value>@%ID_Value</value>
</NameValueBoolean>
<NameValuePassword>
<name>Global/Text2/ID</name>
<value>@%ID_Value</value>
</NameValuePassword>
<NameValuePassword>
<name>Global/Text3/ID</name>
<value>@%ID_Value</value>
</NameValuePassword>
<NameValuePassword>
<name>Global/Text4/ID</name>
<value>%&ID_Value</value>
</NameValuePassword>
</Root>

Desired Output:

Code:
<Root>
<NameValueBoolean>
<name>Global/Text1/ID</name>
<value>New Value</value>
</NameValueBoolean>
<NameValuePassword>
<name>Global/Text2/ABC</name>
<value>@%ID_Value</value>
</NameValuePassword>
<NameValuePassword>
<name>Global/Text3/ID</name>
<value>New Value</value>
</NameValuePassword>
<NameValuePassword>
<name>Global/Text4/ID</name>
<value>%&ID_Value</value>
</NameValuePassword>
</Root>

Thanks,
Krishna

Last edited by mailing2vamsi; 09-12-2012 at 04:24 PM..
# 5  
Old 09-12-2012
Another way to do this is with ed:
Code:
#!/bin/ksh
ed -s in.xml <<-EOF
        gX<name>.*/ID<X.+1s/@%[^<]*/New Value/
        w out.xml
EOF

If you want to overwrite the input file instead of creating a new file, just change the 2nd line of the ed script from w out.xml to w.

I use ksh, but at least sh and bash will also work here.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 09-14-2012
Thanks for all your suggestions, but I am failing to full fill the requirements.

XML data is in a file and modified data should be updated to same file. only data having ">#!...<" needs to be changed if Element1, Element2 and user are available previous line. please find the original and output below:

Can some please help me to break this, thanks in advance.

Original:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<repoInstanceName>%%DOMAIN%%-PrintFileProcessorBW</repoInstanceName>
<NVPairs>
<NameValuePairPassword>
<name>Message/Element1/MIG</name>
<value>#!YEkZA=</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element2/MIG</name>
<value>#!FQKi0=</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element3/MIG</name>
<value>#!FQKi0=</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element1/MIG</name>
<value>#!Lo44o=</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element2/MIG</name>
<value>#!HkCM=</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element3/MIG</name>
<value>#!FQKi0=</value>
</NameValuePairPassword>
</NVPairs>
<repoInstances selected="local">
<httpRepoInstance>
<user>ABC</user>
<password>#!JQLYU</password>
</httpRepoInstance>
<rvRepoInstance>
<user>ABC</user>
<password>#!WiRN4</password>
</rvRepoInstance>
</repoInstances>
</application>

Modified:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<repoInstanceName>%%DOMAIN%%-PrintFileProcessorBW</repoInstanceName>
<NVPairs>
<NameValuePairPassword>
<name>Message/Element1/MIG</name>
<value>#!AAAAA</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element2/MIG</name>
<value>#!AAAAA</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element3/MIG</name>
<value>#!FQKi0=</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element1/MIG</name>
<value>#!AAAAA</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element2/MIG</name>
<value>#!AAAAA</value>
</NameValuePairPassword>
<NameValuePairPassword>
<name>Message/Element3/MIG</name>
<value>#!FQKi0=</value>
</NameValuePairPassword>
</NVPairs>
<repoInstances selected="local">
<httpRepoInstance>
<user>ABC</user>
<password>#!AAAAA</password>
</httpRepoInstance>
<rvRepoInstance>
<user>ABC</user>
<password>#!AAAAA</password>
</rvRepoInstance>
</repoInstances>
</application>

# 7  
Old 09-14-2012
You can use this sed:
Code:
sed '/line 1 pattern/{
  N;
  /\nline 2 pattern/ {
    s/pattern/replace/
  }
}' file

please note when you use a "N" the pattern is in two line with \n separator. So you have to take care of your "s" command. And you have to know this sed doesn't work if you have a file with :

Code:
pattern1
pattern1
pattern2

For your last example you can do:
Code:
sed '/^<user>/{                                                  
N;
s/\n<password>.*</\n<password>#!AAAAA</
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string - searching from input file

Hi I need help with writing a script to change a string in a file. The script needs to read an input list (list.txt) file line by line searching for that string in a text.file. Once the string is found the last few words in the string should be replaced. eg list.txt will contain hello my... (6 Replies)
Discussion started by: sudobash
6 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Replace the second pattern alone in VI.

I have lines like: table10 table_name_10 table10 table_name_10 table20 table_name_20 table20 table_name_20 table30 table_name_30 table30 table_name_30 I want to change the second "table_names" in all lines to test_table_name. Required output would be: table10 table_name_10 table10... (19 Replies)
Discussion started by: gctex
19 Replies

3. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

4. Shell Programming and Scripting

Replace a string pattern

Hi, I have a CSV with following type of data and would like to replace the timestamp information with 'null' string. Can you please suggest me on same? 8,1,'1','1',11,'2013-08-12 18:34:17.0','null',1,'2013-08-12 18:34:17.0','null','PROMOTIONAL','12','1','11','11',11,'0' Thanks for your... (10 Replies)
Discussion started by: bhupinder08
10 Replies

5. Shell Programming and Scripting

Searching a particular string pattern in 10000 files

Problem Statement:- I need to search a particular `String Pattern` in around `10000 files` and find the records which contains that `particular pattern`. I can use `grep` here, but it is taking lots of time. Below is the command I am using to search a `particular string pattern` after... (3 Replies)
Discussion started by: raihan26
3 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Searching String from set of similar File pattern from the Dir

Guys, Here is the script that searches string from the set of similar files from the log directory, All the file patterns are defined as input file, from where the script should map to those files in the LOG_DIR and should start searching the strings from all those similar files. ... (1 Reply)
Discussion started by: raghunsi
1 Replies

8. Shell Programming and Scripting

Pattern searching and replace

I have data this data in a text file 1 PSE480 (P) 2 PSE600 (P) 3 (P) PSE600 4 (P) PSE720 5 PSE600 (P) 6 PSE720 (P) 7 x12(P)PSE360 8 PSE450 (P) 9 PSE540 (P) 10 PSE720 (P) 11 (P) PSE1440 12 24sPSE720 (P) What i want id the last 3 (or 4 in one case) characters after PSE my final... (2 Replies)
Discussion started by: lifzgud
2 Replies

9. Shell Programming and Scripting

Match pattern and replace with string

hi guys, insert into /*<new>*/abc_db.tbl_name this is should be replaced to insert into /*<new>*/${new}.tbl_name it should use '.' as delimiter and replace is there any way to do it using sed (6 Replies)
Discussion started by: sol_nov
6 Replies

10. UNIX for Dummies Questions & Answers

Searching for files with certain string pattern

Hello All I would like to search for files containing certain string pattern under all the directories under /vobs/vobname and print the output to a file in my home directory. How can I do this? Note: /vobs/vobname conatins several directories. Thank You in advance newbetounix (1 Reply)
Discussion started by: intrigue
1 Replies
Login or Register to Ask a Question