Replacing number between xml tags with ksh shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing number between xml tags with ksh shell script
# 1  
Old 10-05-2011
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:
Code:
<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.
Code:
<tag>12345678901235</tag>

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

Thanks!

Last edited by vbe; 10-05-2011 at 01:45 PM.. Reason: You do know we use code tags for code/data dont you?
# 2  
Old 10-05-2011
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
# 3  
Old 10-05-2011
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
# 4  
Old 10-05-2011
Code:
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

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

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 10-06-2011
Thanks alot for the response. Your solution works. Smilie 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

Code:
  
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

# 6  
Old 10-06-2011
Code:
# echo "<tag>12345678901234</tag>"|sed 's|\(<[^<]*>.\{13\}\).\([^>]*>\)|\1NEW\2|'
<tag>1234567890123NEW</tag>

# 7  
Old 10-06-2011
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 Smilie

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read xml tags and then remove the tag using shell script

<Start> <Header> This is header section </Header> <Body> <Body_start> This is body section <a> <b> <c> <st>111</st> </c> <d> <st>blank</st> </d> </b> </a> </Body_start> <Body_section> This is body section (3 Replies)
Discussion started by: RJG
3 Replies

2. Shell Programming and Scripting

Getting error while including values in xml tags using shell script

Hi All, Please find the code below where I want to add the variable value in between the XML tags. I am taking one string and my goal is to put them between the xml tags. Ex : in between <name> , <lname> Kindly suggest a correction because while executing this script I am getting and... (8 Replies)
Discussion started by: rajneesh4U
8 Replies

3. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 Replies

4. Shell Programming and Scripting

Reading xml tags from ksh script

Hi, I have an XMl file, below is sample: <TRANSFORMATION DESCRIPTION ="Created by:- " NAME ="LKP_FT_T_FILEK" OBJECTVERSION ="1" REUSABLE ="YES" TYPE ="Lookup Procedure" VERSIONNUMBER ="1"> </TRANSFORMATION> I need to read the tag, and if the tag is TRANSORMATION, i want to check the Type... (6 Replies)
Discussion started by: kedar_laveti
6 Replies

5. Shell Programming and Scripting

Shell script to extract data in repeating tags from xml

Hi, I am new to shell scripting. I need to extract data between repeating tags from an xml file and store the data in an array to process it further. <ns1:root xmlns:ns1="http://example.com/config"> <ns1:interface>in1</ns1:interface> <ns1:operation attribute1="true" attribute2="abd"... (2 Replies)
Discussion started by: sailendra
2 Replies

6. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (3 Replies)
Discussion started by: sene_geet
3 Replies

7. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (1 Reply)
Discussion started by: sene_geet
1 Replies

8. Shell Programming and Scripting

Replacing part of XML code inside comment tags

Hello! I'd like to modify custom values in a XML config file between comment tags using bash script. <feature> <keyboardshortcut>C-m</keyboardshortcut> <option1>disabled</option2> <option2>enabled</option2> </feature> <!-- bash script features START --> <feature> ... (2 Replies)
Discussion started by: prism1
2 Replies

9. Shell Programming and Scripting

Replacing the last record in xml with different tags

I have special requirement, my system provided the xml file as below(available xml file) and I need to convert it as below desired xml file. is it possible thru shell scripts or awk? What I need is : my available xml contains number of records with tags <RevenueAmounts>, the last of record is... (6 Replies)
Discussion started by: LinuxLearner
6 Replies

10. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies
Login or Register to Ask a Question