Find the answer to your Linux question:
Results 1 to 6 of 6
Hey, I would like to add a tag <here> </here> below the second occurrence of <bb> tag. Code: <aa> </aa> <bb> </bb> -- 1st occurrence <cc> </cc> <bb> </bb> -- ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! AceAll's Avatar
    Join Date
    May 2005
    Posts
    67

    sed question


    Hey,


    I would like to add a tag <here> </here> below the second occurrence of <bb> tag.

    Code:
    <aa> </aa>
    <bb> </bb> -- 1st occurrence 
    
    <cc> </cc> 
    
    <bb>      </bb>   -- 2nd occurrence
    
     <here> </here>
    
    <dd> </dd>
    I tried this one, but in vain. Any idea?

    Code:
    sed -en '/<bb>/2' -e '/<bb>/a<here> </here>/' test

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    If awk is allowed:
    Code:
    awk '/<bb>/ && ++c==2 {$0=$0 RS "<here> </here>"}1' file

  3. #3
    Just Joined! AceAll's Avatar
    Join Date
    May 2005
    Posts
    67
    Thanks for your update. If possible, please explain the awk command that you have used ...

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by AceAll View Post
    Thanks for your update. If possible, please explain the awk command that you have used ...
    Ok, here we go.

    Code:
    awk '/<bb>/ && ++c==2 {$0=$0 RS "<here> </here>"}1'
    Explanation:

    Code:
    /<bb>/ && ++c==2
    If a line contains <bb> increase the counter c. On the second match if the counter has the value of 2:

    Code:
    {$0=$0 RS "<here> </here>"}
    add the new line behind the 2nd line with <bb>

    Code:
    1
    Print the current line.

  5. #5
    Just Joined! AceAll's Avatar
    Join Date
    May 2005
    Posts
    67
    Thanks Franklin52!!

  6. #6
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Why not XSLT transform the document?

Posting Permissions

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