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:
<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 @%.
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.
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 :
pattern1
pattern1
pattern2
For your last example you can do:
sed '/^<user>/{
N;
s/\n<password>.*</\n<password>#!AAAAA</
}' file
writes to same file and it will allow to replace a string with other, but really not aware on how to place a condition before repalce.
If some option to search for a value and then to next line, it will be helpful.
It is never a good idea to edit your originals. What if there's a bug in your program? You've wrecked your input data! Make a new file and check, or at least back up first.
I agree with Corona688 that it is never a good idea to edit your originals without a backup.
I am also concerned that every post you make changes what you want to do. First we're changing a value tagged "@%ID_Value" in a line following a line that contains a name tagged string ending in "/ID" to an unspecified value. Then the new string to replace "@%ID_Value" is specified to be "New Value". And, now you want to change ">#!" followed by any characters up to the last "<" on the line to (no matter what tags are used on the line following a line that contains Element1, Element2, or user (no matter what tags appear on that line).
This sounds like a very dangerous set of changes to make since "user" could easily appear as a value in a data element in your XML file as well as being a tag, but the following ed script will do what you're asking for after saving a backup copy of your original file (assuming your original xml file is named in.xml . If your xml file has a different name, change in.xml in the following script to the name of the xml file you want to update. The backup copy of your file will have the name of your xml file with an underscore character prepended (e.g., _in.xml ):
#!/bin/ksh
file=in.xml
ed -s $file <<-EOF
w _$file
g/Element[12]/.+1s/>#!.*</>#!AAAAA</
g/user/.+1s/>#!.*</>#!AAAAA</
w
EOF
It worked, thanks to every one for your help and suggestions.
---------- Post updated at 05:59 PM ---------- Previous update was at 11:21 AM ----------
Hey would like to check one more thing. Can we have case sensitive check in ed as an additional requirement I can get first comparision element as "Element" or "ELEMENT" or "element"