Skip to main content
Total overhaul: corrected spelling, fixed grammar, improved formatting, clarified ambiguity.
Source Link

Extract data between two xml tagsthe Children of a Specific XML Element Type

Extract data between two xmlGiven a specific XML element (i.e. a specific tag name) and a snippet of XML data, I want to extract the children from each occurrence of that element. More specifically, I have the following snippet of (not quite valid) XML data:

<!-- data.xml -->

<instance ab=1 >
    <a1>aa</a1>
    <a2>aa</a2>
</instance>
<instance ab=2 >
    <b1>bb</b1>
    <b2>bb</b2>
</instance>
<instance ab=3 >
    <c1>cc</c1>
    <c2>cc</c2>
</instance>

Xml FileI would like a script or command which takes this data as input and produces the following output:

<instance ab=1 >
<a1>aa</a1>
    <a2>aa</a2>
</instance>
<instance ab=2 >
<b1>bb</b1>
    <b2>bb</b2>
</instance>
<instance ab=3 >
<c1>cc</c1>
    <c2>cc</c2>
</instance>
<a1>aa</a1><a2>aa</a2>
<b1>bb</b1><b2>bb</b2>
<c1>cc</c1><c2>cc</c2>

LookingI would like for output by usingthe solution to use standard text-processing tools such as sed or awk like following.

<a1>aa</a1><a2>aa</a2>
<b1>bb</b1><b2>bb</b2>
<c1>cc</c1><c2>cc</c2>

It's not working withI tried using the following sed command., but it did not work:

sed -n '/<Sample/,/<\/Sample/p' abdata.xml

Extract data between two xml tags

Extract data between two xml tag.

Xml File:

<instance ab=1 >
<a1>aa</a1>
    <a2>aa</a2>
</instance>
<instance ab=2 >
<b1>bb</b1>
    <b2>bb</b2>
</instance>
<instance ab=3 >
<c1>cc</c1>
    <c2>cc</c2>
</instance>

Looking for output by using sed or awk like following.

<a1>aa</a1><a2>aa</a2>
<b1>bb</b1><b2>bb</b2>
<c1>cc</c1><c2>cc</c2>

It's not working with following sed command.

sed -n '/<Sample/,/<\/Sample/p' ab

Extract the Children of a Specific XML Element Type

Given a specific XML element (i.e. a specific tag name) and a snippet of XML data, I want to extract the children from each occurrence of that element. More specifically, I have the following snippet of (not quite valid) XML data:

<!-- data.xml -->

<instance ab=1 >
    <a1>aa</a1>
    <a2>aa</a2>
</instance>
<instance ab=2 >
    <b1>bb</b1>
    <b2>bb</b2>
</instance>
<instance ab=3 >
    <c1>cc</c1>
    <c2>cc</c2>
</instance>

I would like a script or command which takes this data as input and produces the following output:

<a1>aa</a1><a2>aa</a2>
<b1>bb</b1><b2>bb</b2>
<c1>cc</c1><c2>cc</c2>

I would like for the solution to use standard text-processing tools such as sed or awk.

I tried using the following sed command, but it did not work:

sed -n '/<Sample/,/<\/Sample/p' data.xml
edited body
Source Link
Abhi S
  • 31
  • 1
  • 4

Extract data between two xml tag.

Xml File:

<instance ab=1 >
<a1>a1<<a1>aa</a1>
    <a2>a2<<a2>aa</a2>
</instance>
<instance ab=2 >
<b1>a1<<b1>bb</b1>
    <b2>a2<<b2>bb</b2>
</instance>
<instance ab=3 >
<c1>a1<<c1>cc</c1>
    <c2>a2<<c2>cc</c2>
</instance>

Looking for output by using sed or awk like following.

<a1>a1<<a1>aa</a1><a2>a1<a1><a2>aa</a2>
<b1>a1<<b1>bb</b1><b2>a2<b1><b2>bb</b2>
<c1>a1<<c1>cc</c1><c2>a2<c1><c2>cc</c2>

It's not working with following sed command.

sed -n '/<Sample/,/<\/Sample/p' ab

Extract data between two xml tag.

Xml File:

<instance ab=1 >
<a1>a1</a1>
    <a2>a2</a2>
</instance>
<instance ab=2 >
<b1>a1</b1>
    <b2>a2</b2>
</instance>
<instance ab=3 >
<c1>a1</c1>
    <c2>a2</c2>
</instance>

Looking for output by using sed or awk like following.

<a1>a1</a1><a2>a1</a2>
<b1>a1</b1><b2>a2</b2>
<c1>a1</c1><c2>a2</c2>

It's not working with following sed command.

sed -n '/<Sample/,/<\/Sample/p' ab

Extract data between two xml tag.

Xml File:

<instance ab=1 >
<a1>aa</a1>
    <a2>aa</a2>
</instance>
<instance ab=2 >
<b1>bb</b1>
    <b2>bb</b2>
</instance>
<instance ab=3 >
<c1>cc</c1>
    <c2>cc</c2>
</instance>

Looking for output by using sed or awk like following.

<a1>aa</a1><a2>aa</a2>
<b1>bb</b1><b2>bb</b2>
<c1>cc</c1><c2>cc</c2>

It's not working with following sed command.

sed -n '/<Sample/,/<\/Sample/p' ab
edited body
Source Link
user232326
user232326
edited body
Source Link
Abhi S
  • 31
  • 1
  • 4
Loading
edited title
Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117
Loading
Rollback to Revision 2
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117
Loading
edited title
Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117
Loading
added 49 characters in body
Source Link
Abhi S
  • 31
  • 1
  • 4
Loading
added 2 characters in body
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117
Loading
Source Link
Abhi S
  • 31
  • 1
  • 4
Loading