ID generation in sequence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ID generation in sequence
# 1  
Old 11-12-2009
ID generation in sequence

i have an xml tag. The value for the tag should be iterated through out the xml document.
eg:
Code:
<data><id><id><name>a</name><addr>aaa</addr><phnumb>3456</phnumb><state>ca</state><city>ny</city></data>
<data><id><id><name>b</name><addr>bbb</addr><phnumb>4567</phnumb><state>la</state><city>nj</city></data>
<data><id><id><name>c</name><addr>ccc</addr><phnumb>5678</phnumb><state>nv</state><city></city></data>
<data><id><id><name>d</name><addr>ddd</addr><phnumb>6789</phnumb><state>oh</state><city></city></data>
<data><id><id><name>e</name><addr>eee</addr><phnumb>7890</phnumb><state>al</state><city></city></data>

the 'id' tag in the above example has to be generated sequentially. so the required result is
Code:
<data><id>1<id><name>a</name><addr>aaa</addr><phnumb>3456</phnumb><state>ca</state><city>ny</city></data>
<data><id>2<id><name>b</name><addr>bbb</addr><phnumb>4567</phnumb><state>la</state><city>nj</city></data>
<data><id>3<id><name>c</name><addr>ccc</addr><phnumb>5678</phnumb><state>nv</state><city></city></data>
<data><id>4<id><name>d</name><addr>ddd</addr><phnumb>6789</phnumb><state>oh</state><city></city></data>
<data><id>5<id><name>e</name><addr>eee</addr><phnumb>7890</phnumb><state>al</state><city></city></data>

the number of records in the xml is unbounded, so first i need to find the number the id tags in the document and then generate the value for it in sequence

Last edited by radoulov; 11-12-2009 at 10:38 AM.. Reason: Use code tags, please!
# 2  
Old 11-12-2009
Code:
perl -pe'
  s/<id><id>/"<id>".++$c."<id>"/e
  ' infile

If you want to edit in-place:

Code:
perl -i.bck -pe'
  s/<id><id>/"<id>".++$c."<id>"/e
  ' infile

I believe it should be <id>n</id> Smilie
# 3  
Old 11-12-2009
If all lines are same format,
Code:
awk '{gsub(/<id><id>/,"<id>"NR"<id>"); print }' urfile
awk '{gsub(/<id><id>/,"<id>"NR"<id>")}1' urfile

If not,
Code:
awk 'BEGIN {id=1} {gsub(/<id><id>/,"<id>"id++"<id>"); print }' urfile
awk 'BEGIN {id=1} {gsub(/<id><id>/,"<id>"id++"<id>")}1' urfile

# 4  
Old 11-12-2009
hi....

there is a small error in the data that i provided...since this being an xml document it has a closed tag. so it should be <id></id>

and also the entire document is in a single line with multiple <id></id> tags

---------- Post updated at 07:14 PM ---------- Previous update was at 07:02 PM ----------

Quote:
Originally Posted by radoulov
Code:
perl -pe'
  s/<id><id>/"<id>".++$c."<id>"/e
  ' infile

If you want to edit in-place:

Code:
perl -i.bck -pe'
  s/<id><id>/"<id>".++$c."<id>"/e
  ' infile

I believe it should be <id>n</id> Smilie
hi error is thrown when i try to execute the below code
perl -pe'
s/<id></id>/"<id>".++$c."</id>"/e
' infile

how can this be handled?

and also the entire xml will ne in a single line with delimiters between each record like

<data><id></id><name></name><city></city></data>@@#<data><id></id><name></name><city></city></data>@@#<data><id></id><name></name><city></city></data>@@#<data><id></id><name></name><city></city></data>@@#

---------- Post updated at 07:54 PM ---------- Previous update was at 07:14 PM ----------

hi,

i tweaked in the code to generate the id but, i still am left with <id><id>

can some one help me with a code to find and replace the following condition:

<id>1<id>
<id>2<id>
<id>3<id>
<id>4<id>
<id>5<id>.....

to

<id>1</id>
<id>2</id>
<id>3</id>
<id>4</id>
<id>5</id>
# 5  
Old 11-13-2009
Ok, if you don't do any research by yourself, and only wait for solution, then

I will not reply your post again.
# 6  
Old 11-13-2009
Code:
while(<DATA>){
	s/(?<=<id>)(?=<id>)/$./e;
	print;
}
__DATA__
<data><id><id><name>a</name><addr>aaa</addr><phnumb>3456</phnumb><state>ca</state><city>ny</city></data>
<data><id><id><name>b</name><addr>bbb</addr><phnumb>4567</phnumb><state>la</state><city>nj</city></data>
<data><id><id><name>c</name><addr>ccc</addr><phnumb>5678</phnumb><state>nv</state><city></city></data>
<data><id><id><name>d</name><addr>ddd</addr><phnumb>6789</phnumb><state>oh</state><city></city></data>
<data><id><id><name>e</name><addr>eee</addr><phnumb>7890</phnumb><state>al</state><city></city></data>

# 7  
Old 11-13-2009
Quote:
Originally Posted by summer_cherry
Code:
while(<DATA>){
    s/(?<=<id>)(?=<id>)/$./e;
    print;
}
__DATA__
<data><id><id><name>a</name><addr>aaa</addr><phnumb>3456</phnumb><state>ca</state><city>ny</city></data>
<data><id><id><name>b</name><addr>bbb</addr><phnumb>4567</phnumb><state>la</state><city>nj</city></data>
<data><id><id><name>c</name><addr>ccc</addr><phnumb>5678</phnumb><state>nv</state><city></city></data>
<data><id><id><name>d</name><addr>ddd</addr><phnumb>6789</phnumb><state>oh</state><city></city></data>
<data><id><id><name>e</name><addr>eee</addr><phnumb>7890</phnumb><state>al</state><city></city></data>

Yes, this should work if every line contains the id tag and you can even omit the e modifier.

---------- Post updated at 10:38 AM ---------- Previous update was at 10:35 AM ----------

Quote:
Originally Posted by Sgiri1
[...]
hi error is thrown when i try to execute the below code
perl -pe'
s/<id></id>/"<id>".++$c."</id>"/e
' infile

[...]
Code:
% cat infile
<data><id></id><name></name><city></city></data>@@#<data><id></id><name></name><city></city></data>@@#<data><id></id><name></name><city></city></data>@@#<data><id></id><name></name><city></city></data>@@#

Code:
% perl -pe' 
  s|<id></id>|"<id>".++$c."</id>"|ge
  ' infile
<data><id>1</id><name></name><city></city></data>@@#<data><id>2</id><name></name><city></city></data>@@#<data><id>3</id><name></name><city></city></data>@@#<data><id>4</id><name></name><city></city></data>@@#

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with file generation

Dear all Hope you are doing good. I've requirement where in I need to generate a file containing set of SQLs as per the inputs. My script flow is like reading few parameters using read command to read input to script and process the inputs. However at one of the input, my script has to... (3 Replies)
Discussion started by: tenderfoot
3 Replies

2. Shell Programming and Scripting

Sequence generation in shell

Hi, I am using the nested for loops to generate the sequence , taking start and end sequence number input from test.txt (sample content mentioned below). Also , can I print the rest of columns as well with sequence number into the same file. for i in `cat test.txt|cut -d"," -f7` do ... (8 Replies)
Discussion started by: tushar.modgil
8 Replies

3. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

4. Shell Programming and Scripting

Character generation

I need a (bash) script to echo to the screen all possible 4 character hex combinations of: A-F, 0-9. I beleive there are 65,535 approx combos. Output something like this: 0001 0002 0003 0004 0005 0006 0007 0008 0009 000A ... ... FFFE FFFF (6 Replies)
Discussion started by: ajp7701
6 Replies

5. Shell Programming and Scripting

Config Generation

I'm looking for good ways to handle config generation. I have been rolling my own for a few years now, but I'm wondering if there is a better way and would like to get some advice from anyone who has any ideas or experience in doing this. Here are some things I want to achieve in generating... (1 Reply)
Discussion started by: humbletech99
1 Replies

6. Shell Programming and Scripting

Help with excelsheet generation

Hi All, i have around 50 queries in sybase. We have a requirement where we need to write a unix script, which execute the query one by one & generate the excel sheet & send it to user. I have completed half of the part, where i am executing query one by one & putting the result into a .txt... (4 Replies)
Discussion started by: Amit.Sagpariya
4 Replies

7. Shell Programming and Scripting

Graph generation

How can I generate graphs using perl in unix solaris environment? Please suggest. (2 Replies)
Discussion started by: wadhwa.pooja
2 Replies

8. Shell Programming and Scripting

Sequence number generation on a key column

Hi Folks, Can you help me with this issue: I have to generate the numbers say from 1001 for each record in a file based on a key field, the catch is the generated number should be unique based on key column. (EMP_NUMBER) Example: Input File: EMP_NUMBER EMP_NAME 8908 ... (6 Replies)
Discussion started by: sbasetty
6 Replies

9. Programming

Shellcode Generation using C

Hi, I was just learning about buffer overflow attacks... I was curious as to how to generate a simple shellcode. I've written two codes - One is the typical program that has a vulnerability inside and the other is the shellcode. main program: void test(); int main() { test(); ... (2 Replies)
Discussion started by: Legend986
2 Replies
Login or Register to Ask a Question