Replacing number between xml tags with ksh shell script

Hallo,

im basically a complete noob on shell scripting and im trying to replace or rather add 1 to a number between xml tags.

The xml basically has a tag somewhere that looks like this:

<tag>12345678901234</tag>

Now i want to replace the number between the tags. And i want the file to contain the following after the shell command.

<tag>12345678901235</tag>

Any help would be greatly appreciated. Also links to decent tutorials for noobs like me :).

Thanks!

Do you want to replace with some other value or do you want to increment it?

--ahamed

---------- Post updated at 08:56 AM ---------- Previous update was at 08:54 AM ----------

It would be better if you paste the actual data rather than a sample like this. Trust me it will make a difference!

--ahamed

Both would be fine, i guess the best looking would be to replace it with a random number with the same lenght. But just incrementing it by one would do.

Thanks in advance

sed 's/[0-9][0-9]*/new_val/g' input.xml

--ahamed

---------- Post updated at 09:04 AM ---------- Previous update was at 09:01 AM ----------

or

sed 's/\(<.*>\)[0-9][0-9]*\(<\/.*>\)/\1new_val\2/g' input.xml

--ahamed

Thanks alot for the response. Your solution works. :slight_smile: However, would it be possible to replace new_val by a piece of code that inserts a random number at lenght 14? Preferably not starting with a 0.

---------- Post updated at 02:40 AM ---------- Previous update was at 01:58 AM ----------

I created something looking like this. Currently im at a loss how to add the lenght 13 array to the value 1 that is yet present in new_val

  
x=0
new_val=1
imsiarray[13]
 
#Generate unique IMSI ID
 while ((x < 13)) ; do
         imsiarray[x]=$RANDOM
       x = x+1
  done
 
#here imsiarray needs to be converted to new_val
 
sed 's/\(<ns2:imsi>\)[0-9][0-9]*\(<\/ns2:imsi>\)/\1'$new_val'\2/g' $dateiundpfad
# echo "<tag>12345678901234</tag>"|sed 's|\(<[^<]*>.\{13\}\).\([^>]*>\)|\1NEW\2|'
<tag>1234567890123NEW</tag>

I think i found a solution myself which turned out to be quite easy.
It wont win an award for beautifull code but it should work :slight_smile:

imsi=''
imsi=$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM
new_val=expr $imsi 1 14

sed 's/\(<ns2:imsi>\)[0-9][0-9]*\(<\/ns2:imsi>\)/\1'$new_val'\2/g' $dateiundpfad