Need Help in Index


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help in Index
# 1  
Old 12-08-2015
Need Help in Index

I am having data in XML format and trying to extract codes form two fields called <String>, below is the data.
Code:
<Node>tollfree<Condition>BooleanOperator<Operation>AND</Operation><Condition>BooleanOperator<Operation>NOT</Operation><Condition>FieldSelection<Field Context="ALL fields" condition="StringList" field="Other-Party-Id-Address"/><String>530101,530103,530105,509091,509092,509093,509095,530100,509099</String><Comparation>3</Comparation><MatchCase>true</MatchCase></Condition></Condition><Condition id="H_FREE">FieldSelection<Field Context="ALL fields" condition="StringList" field="Other-Party-Id-Address"/><String>53546,50878,511150,51985,58595,563636,51980,50605,50405,55501,55255,55502,56694,55436,55437,518180,54334,58888084,55511,55522,563000,55507,55530,55541,55577,58327,54300,5303020,543210,554562,55323,50325,54400,511310,56660,56789,57323,52347,544111,55500,51802,54200,542001,57878,52000,50234,55694,54422,51121,54054,554356,53111,59888,53010,51477,51144,55321,55256,51967,50909,57677,1950,55177,52409,52256,52027,161,52134,58888111,58888511,58888711</String><Comparation>0</Comparation><MatchCase>true</MatchCase></Condition></Condition><Tariff>Rate<UnitType>Service Specific</UnitType><Price>0.0<Factor>1</Factor></Price><Interval>1<Factor>1</Factor></Interval><UpdateType>Active</UpdateType></Tariff></Node>

So far I had tried to extract the data betwen first <String> i.e
Code:
530101,530103,530105,509091,509092,509093,509095,530100,509099

.
But I am unable to extract
Code:
<String>53546,50878,511150,51985,58595,563636,51980,50605,50405,55501,55255,55502,56694,55436,55437,518180,54334,58888084,55511,55522,563000,55507,55530,55541,55577,58327,54300,5303020,543210,554562,55323,50325,54400,511310,56660,56789,57323,52347,544111,55500,51802,54200,542001,57878,52000,50234,55694,54422,51121,54054,554356,53111,59888,53010,51477,51144,55321,55256,51967,50909,57677,1950,55177,52409,52256,52027,161,52134,58888111,58888511,58888711</String>

information.

and the script Iused is
Code:
awk -F"," 'BEGIN{OFS=","}
{
if ($0 ~ "<Node")
  {
  a = index($0,"<String>")
  z = index($0,"</String>")
  print substr($0, a+8, z-(a+8)) > "Voice1.txt" }
  }' Voice2.txt

Please help me in getting the next missing information.
# 2  
Old 12-08-2015
You'll need to repeat the search:
Code:
awk -F"," '
BEGIN           {OFS=","}
$0 ~ "<Node"    {match ($0, /<String>[^<]*<\/String>/)
                 print substr ($0, RSTART+8, RLENGTH-17)
                 $0 = substr ($0, RSTART + RLENGTH)
                 match ($0, /<String>[^<]*<\/String>/)
                 print substr ($0, RSTART+8, RLENGTH-17)
                }
' file
530101,530103,530105,509091,509092,509093,509095,530100,509099
53546,50878,511150,51985,58595,563636,51980,50605,50405,55501,...

# 3  
Old 12-08-2015
Thank you very much it's working great, and output is as desired.
# 4  
Old 12-08-2015
Just a thought!!!

Why you need to use substr, match.. etc?

Code:
awk -F'<String>|</String>' '/<Node/{for(i=2;i<=NF;i+=2) print $i;}' file_name

-Ranga
# 5  
Old 12-08-2015
Or try
Code:
awk -F"," '
BEGIN   {OFS=","}
/<Node/ {while (match ($0, /<String>[^<]*<\/String>/))  {print substr ($0, RSTART+8, RLENGTH-17)
                                                         $0 = substr ($0, RSTART + RLENGTH)
                                                        }
        }
' file

---------- Post updated at 13:47 ---------- Previous update was at 13:46 ----------

or a small adaption of rangarasan's porposal:
Code:
awk -F'<.?String>' '/<Node/{for(i=2;i<=NF;i+=2) print $i;}' file

# 6  
Old 12-08-2015
Extract first String, works on OSX:

Code:
awk  '{print $2}' FS="<String>|</String>" file

# 7  
Old 12-08-2015
Try:
Code:
$ awk '$1=="String"{print $2}' RS=\< FS=\> file
530101,530103,530105,509091,509092,509093,509095,530100,509099
53546,50878,511150,51985,58595,563636,51980,50605,50405,55501,55255,55502,56694,55436,55437,518180,54334,58888084,55511,55522,563000,55507,55530,55541,55577,58327,54300,5303020,543210,554562,55323,50325,54400,511310,56660,56789,57323,52347,544111,55500,51802,54200,542001,57878,52000,50234,55694,54422,51121,54054,554356,53111,59888,53010,51477,51144,55321,55256,51967,50909,57677,1950,55177,52409,52256,52027,161,52134,58888111,58888511,58888711

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Duplicate value with different index

Hello Gents, Please give a help with this case Input 10001010G1 10001010G1 10001010G1 10001010G2 10001010G3 10001012G1 10001012G1 10001012G1 10001012G1 10001014G1 10001014G1 10001014G2 (5 Replies)
Discussion started by: jiam912
5 Replies

2. UNIX and Linux Applications

Index server

Hi guys, I have postgresql server with huge amount of data, nearly 2 billion records. each record is at most 50 bytes(4 integer fields). I need to build index on all column to do fast reporting. but indexes becomes bloat after some time. almost 80% of database size is because of its huge... (0 Replies)
Discussion started by: majid.merkava
0 Replies

3. Shell Programming and Scripting

Get index value by awk

HI All, I would like to pass a integer and get all values under this index the by using awk. Could anyone help? Thanks :> input: 1,2,3,4,5,6,7 1,2,3,48,5,6,7 1,2,3,4,5,6,7 e.g. i pass 4 to awk command output: 4 48 4 Video tutorial on how to use code tags in The UNIX and Linux... (8 Replies)
Discussion started by: mimilaw
8 Replies

4. Shell Programming and Scripting

awk index

1 2 000060000 How do i return the point in the string where the 6 is? i.e what I want on output is 1 2 5 something like awk '{print $1 $2 index($3,6) }' but I can't get it to work Thanks in advance (3 Replies)
Discussion started by: garethsays
3 Replies

5. Shell Programming and Scripting

Sort from start index and end index in line

Hi All, I have a file (FileNames.txt) which contains the following data in it. $ cat FileNames.txt MYFILE17XXX208Sep191307.csv MYFILE19XXX208Sep192124.csv MYFILE20XXX208Sep192418.csv MYFILE22XXX208Sep193234.csv MYFILE21XXX208Sep193018.csv MYFILE24XXX208Sep194053.csv... (5 Replies)
Discussion started by: krish_indus
5 Replies

6. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

7. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies

8. Filesystems, Disks and Memory

Backup Index

Hi all, I am using Legato networker for my backups, I need to restore some data from 2001. When doing an inventory on the tape is picks up the label but under pool it says "not in media index". When doing: nsrck -t 01Jan2002 -L7 i get the following: nsrck: checking index for '$client'... (2 Replies)
Discussion started by: macgre_r
2 Replies

9. Shell Programming and Scripting

What is index?

hi, :) In a shell script i came accross the following lines 1.for i in ` find /home/oracle -name ch' 2.do 3.echo $i 4.idx=`expr index $i .` 5.done Here iam not able to understand the porpose of the word "index" in line 4. any help ? cheers RRK (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

10. Shell Programming and Scripting

Index Command

Hi, can anyone explain me how this works (how the flow goes)? Example: CLIENT="UNIXHELP" The second argument passed $2="UNIX" RESULT=`awk -F"=" '/CLIENTS=/ {len = index($2,"'${CLIENT}'");print len }' $2` Thanks in advance. (1 Reply)
Discussion started by: abrd600
1 Replies
Login or Register to Ask a Question