rahul singh
rahul singh

Reputation: 1

XML parsing with shell script

I am having issues while I am trying to get some data from the text filed of the xml tag.

This filed is repeated many times in the xml.

<abc>
<def>
<gh tag="ext" text="1. look
2. for 
3. this 
4. if
5. what">
</gh>
</def>
</abc>

What I have done is to grep the pattern and loop till the "> reached. But I am also getting some other data also. Can someone please help me with that or show me the direction. I tried perl but XML library where missing.

Upvotes: 0

Views: 3041

Answers (4)

jaypal singh
jaypal singh

Reputation: 77105

A quick and dirty solution -

awk '/text="/,/">/{print}' xmlfile | sed -e 's/^\(<gh.*="\)//' -e 's/">$//'

Upvotes: 0

siddardha
siddardha

Reputation: 200

sgrep - Structured Grep - used in searching and indexing XML , HTML.

http://www.cs.helsinki.fi/u/jjaakkol/sgrepexamples.html

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360095

You might be able to adapt the AWK script found here.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798666

Please use the correct tool.

$ xmlstarlet sel -t -m abc/def/gh -v @text input.xml
1. look 2. for 3. this 4. if 5. what

Upvotes: 3

Related Questions