Search Parameter in first line and replace next line content

Hi,

I need help. I have XML file as below

    <a n="infoLevel">
        <v s="true"/>
    </a>
    <a n="localAddr">
        <v s="server.host.com"/>
    </a>
    <a n="ListenPort">
        <v s="21111"/>
    </a>

I need to find variable "ListenPort" in line and then replace next line's 21111 to 98765.

How do I do it using sed or awk or perl?

Thanking you in advance,

With gnu sed:

sed '/ListenPort/ {n; s/21111/98765/}' infile

Other seds might want to have each part on a separate line like:

sed '/ListenPort/{
   n
   s/21111/98765/
}' infile

Using awk,

awk 'f==1 {gsub(/21111/,"98765");f=0; } /ListenPort/ {f++}1' < file >outfile

Thanks dennis.jacob and zaxxon for your time to help me. I am newbie in awk/sed/perl one liner world so unable to understand error on your solution.

I tried SED command as suggested but getting below error.

server:/home> sed '/ListenPort/{ n; s/21111/98765/ }' /tmp/xp.xml
sed: command garbled: /ListenPort/{ n s/21111/98765/ }

I tried AWK command as suggested but getting below error.

server:/home> awk 'f==1 {gsub(/21111/,"98765");f=0; } /ListenPort/ {f++}1' < /tmp/xp.xml > /tmp/xp1.xml
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

---------- Post updated at 12:04 PM ---------- Previous update was at 09:40 AM ----------

I need help. I have XML file as below

Code:

&lt;a n="infoLevel"&gt;
    &lt;v s="true"/&gt;
&lt;/a&gt;
&lt;a n="localAddr"&gt;
    &lt;v s="server.host.com"/&gt;
&lt;/a&gt;
&lt;a n="ListenPort"&gt;
    &lt;v s="21111"/&gt;
&lt;/a&gt;

I need to find variable "ListenPort" in line and then replace next line's 21111 to 98765.

And, this problem is not to solve only one string "21111" but it can be any number between "0-99999". It means displayed problem has 21111 but it could be 1 or 23 or 456 or 7890 or 98711. The good part is starting position of first number is always fixed in that line. It means number always start from position 17 and has surrounded by """ string.

Please help me to solve this problem using sed/awk/perl.

Thanks in advance,

I posted you another version of the sed where every subcommand is in a separate line since older versions of sed do not support writing everything in the same line without ^J for example.

So take the sed script that is more than one line into a script and try it again.

Also I asked you to use code tags - the PM you got explains it very well - you got another one.

dennis' awk might need another version of awk so you might check if you have gawk or if you are on Solaris check for nawk.