0

Given an XML with some blocks ending within the same tag and others with a separate tag:

<parent name="parent_1" team="team_a"> <child name="child_1" team="team_b"/> </parent> <parent name="parent_2" team="team_c"/> <parent name="parent_3" team="team_b"/> 

How can extract the block for a given name?

I have:

awk "/<parent name=\"$name\"/,/<\/parent>/" $file 

which would work for $name=parent_1, and:

awk "/<parent name=\"$name\"/,/\/>/" $file 

which would work for parent_2 or parent_3 but not sure how to do both.

I tried:

awk "/<parent name=\"$name\"/,/[\/>|<\/parent>]/" $file 

as an OR condition but for parent_1 it still gives me:

 <parent name="parent_1" team="team_a"> 

Can that be done?

1
  • 3
    awk is not a XML parser, use xmllint, xmlstarlet or xidelCommentedMar 14, 2023 at 16:27

1 Answer 1

4

With a proper xml parser: xmllint:

file:

<root> <parent name="parent_1" team="team_a"> <child name="child_1" team="team_b"/> </parent> <parent name="parent_2" team="team_c"/> <parent name="parent_3" team="team_b"/> </root> 
$ xmllint --xpath '//parent[@name="parent_1"]' file <parent name="parent_1" team="team_a"> <child name="child_1" team="team_b"/> </parent> 
1
  • Ha - thanks Gilles. This seems way more appropriate indeed
    – Will1v
    CommentedMar 14, 2023 at 16:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.