-1

I have to write a shell script which read my whole xml file and search that if certain conditions are getting fulfilled then it remove some particular lines.

Suppose my xml is in this form

<midget action="D">
    <lists operator="AND">
        <list name="productCode" value="XW143215" actionoperator="in"
            oldoperator="in" type="string" />
        <list name="%%PromotionProductInstanceId%%" value="z" product="S0000001.S0145868"
            actionoperator="in" operator="=" oldoperator="in" type="string" />
        <list name="Type" value="ALL" product="PQRAccess" type="string" />
        <lists id="Prim" />
    </lists>

</midget>

<midget action="Y">
    <lists operator="AND">
        <list name="productCode" value="XW143215" actionoperator="in"
            oldoperator="in" type="string" />
        <list name="Y" value="z" product="S0000001.S0145868"
            actionoperator="in" operator="=" oldoperator="in" type="string" />
        <list name="Type" value="ALL" product="PQRAccess" type="string" />
        <lists id="Trim" />
    </lists>

</midget>

<midget action="D">
    <lists operator="AND">
        <list name="productCode" value="XW143215" actionoperator="in"
            oldoperator="in" type="string" />
        <list name="%%PromotionProductInstanceId%%" value="z" product="S0000001.S0145868"
            actionoperator="in" operator="=" oldoperator="in" type="string" />
        <list name="Type" value="ALL" product="PQRAccess" type="string" />
        <lists id="Trim" />
    </lists>

</midget>

Now my shell script will search for these conditions:

  • Starting element should be <midget
  • on which action="D"
  • then in list element name="%%PromotionProductInstanceId%%"
  • then in list element value="ALL"

Then script should remove both of these statements from midget element.

<list name="%%PromotionProductInstanceId%%" value="z" product="S0000001.S0145868"
            actionoperator="in" operator="=" oldoperator="in" type="string" />
<list name="Type" value="ALL" product="PQRAccess" type="string" />

I have tried sed but i am unable to figure out how to perform search based on above conditions. Any help would be appreciated.

  • 2
    To manipulate XML, I strongly recommend using an XML tools such as XMLStarlet rather than generic tools such as sed. – Stephen Kitt Sep 19 '17 at 5:52
  • 2
    @StephenKitt, obligatory link. ;) – Wildcard Sep 19 '17 at 5:54
  • @StephenKitt But what if I want to achieve this via shell. Isn't it possible. – foxt7ot Sep 19 '17 at 5:57
  • @Wildcard Thanks man but it's a whole other requirement. I have written following code to match and extract those statements. cat $fullPathWithFilename|sed 's/<midget/\n/g'|grep "Trim"|grep 'value="ALL"'|sed 's/<list name="PromotionProductInstanceId".*<lists/<lists/g'|sed 's/^/<midget /g' >> $DIR/temp.xml – foxt7ot Sep 19 '17 at 6:01
  • Also when you are down voting, at least have a courtesy to jot down the reason in comment section so that we don't repeat the mistakes in future.Thanks – foxt7ot Sep 19 '17 at 6:05
3

With XmlStarlet:

xml ed -d '//midget[@action="D"]/*[
    descendant::list[@name="%%PromotionProductInstanceId%%"] and
    descendant::list[@value="ALL"]]/list[
    @name="%%PromotionProductInstanceId%%" or @value="ALL"]' <file.xml

In detail:

  • starting element should be <midget: //midget
  • on which action="D": [@action="D"]
  • then in list element name="%%PromotionProductInstanceId%%": descendant::list[@name="%%PromotionProductInstanceId%%"]
  • then in list element value="ALL": descendant::list[@value="ALL"]
  • then script should remove both of these statements from midget element: list[@name="%%PromotionProductInstanceId%%" or @value="ALL"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.