Editing the next line of the pattern

Hi All,

I am trying to edit the next line when there is a success for a pattern.
For example:

<nvname>abc</nvname>
<nvvalue>1</nvvalue>
<nvname>def</nvname>
<nvvalue>2</nvvalue>

once there is a hit for the string "abc" the value of the nvvalue should be replaced with the value we already have.

the new file should be

<nvname>abc</nvname>
<nvvalue>4</nvvalue>
<nvname>def</nvname>
<nvvalue>2</nvvalue>

I tried several options of sed but I am not able to achieve the same.
The arguments I tried are:
c
d
n
s
g

awk '/>abc</{f=1}f&&!/>abc</{sub(/>.*</,4);f=0}1' inputfile

Hi Yoda I have one issue here I dont have abc as a static string.
I have a variable for this and if I have ' ' quotes then we are not able to pick the value of the variable
also I am a little new to Unix if possible can you please briefly explain the approach you have followed.

If you have string in variable, then assign it to an awk variable:

VAR="abc"

awk -v V="$VAR" '                                       # Assign V = "$VAR"
                {
                        if ( $0 ~ ">" V "<" )           # If pattern ">" V "<" (>abc<) in current record.
                                f = 1                   # If found, set f = 1
                        if ( f && $0 !~ ">" V "<" )     # If f == 1 and pattern ">" V "<" (>abc<) not in current record.
                        {
                                sub(/>.*</, ">4<")      # Substitute everything with in >.*< by ">4<"
                                f = 0                   # Reset f = 0
                        }
} 1 ' file                                              

Hi Yoda I got that thanks
However I have one more issue I am using a command

sed s/$original/$new/g

in this case my new variable is like this

new=<a>123</a>\n<b>345</b>

I want the two xml tags to be in sep line and so I have inserted a \n
this is working fine in echo
however when I do the sed stuff I can see a n in the file itself.
Please let me know if we can actually add a new line

I'm not sure if I got your requirement right.

Let say you have below line in file:

<a>123</a><b>345</b>

To get them into separate lines:

$ awk '/>123</{sub(/></,">\n<")}1' file
<a>123</a>
<b>345</b>