Find the answer to your Linux question:
Results 1 to 6 of 6

Thread: sed question

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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
    If awk is allowed:
    Code:
    awk '/<bb>/ && ++c==2 {$0=$0 RS "<here> </here>"}1' file

  3. #3
    Thanks for your update. If possible, please explain the awk command that you have used ...

  4. $spacer_open
    $spacer_close
  5. #4
    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.

  6. #5
    Thanks Franklin52!!

  7. #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
  •