Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2004
    Posts
    3

    Question Unanswered: Replacing a pattern based on the occurence of another pattern

    Hi,

    I want to do the following

    Assume there is an xml which contains entries like the following...

    <a attr1="value1" attr2="value2"/>
    <b attr1="value3" attr2="value4" attr3="value5"/>
    <a attr1="value1" attr2="value2"/>
    ....


    I want to replace the "attr1" to say "attr3" in every occurence of <a/> tag.

    I need the result to be something like

    <a attr3="value1" attr2="value2"/>
    <b attr1="value3" attr2="value4" attr3="value5"/>
    <a attr3="value1" attr2="value2"/>
    ....

    Is there any tool to do the above?
    Any shell scripts?

    Please reply...

    Thanks and Regards,

    Sriram.C.S

  2. #2
    Join Date
    Aug 2004
    Location
    Rome, Italy
    Posts
    81
    you can use sed on command line or inside of a script this way:

    sed -e '/<a attr1/s/attr1/attr3/' file.xml > file.temp
    mv file.temp file.xml

  3. #3
    Join Date
    Sep 2004
    Posts
    3

    Question

    Hi ducasio,

    My example was simple enough. Actually I wanted something like this

    <a attr1="value1" attr2="value2"/>
    <b attr2="value4" attr1="value3" attr5="value5"/>
    <a attr2="value2" attr1="value1"/>
    <a attr4="valueN" attr2="value2" .....attr1="valueX"... />

    The attr1 need not be placed near start tag <a>. Hence to search by pattern that attr1 is always adjacent to <a> should not be there.

    In such a case how do we go about doing it?

    Please reply.

    Sriram.C.S

  4. #4
    Join Date
    May 2004
    Location
    Barcelona, Spain
    Posts
    54
    Sriram,

    You have to look for /<a/ patterns and then put attr3 instead of attr1.
    It seems pretty straightforward to reach there from ducasio's script.

    Regards

    iaguigon

  5. #5
    Join Date
    Sep 2004
    Posts
    3
    Hi,

    Not sure why I missed a straight forward thing.

    Thanks...ducasio and iaguigon

    Regards,

    Sriram.C.S

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •