Re: Delete only the nth block of text, multi-line

"[email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
If you wanted the 3rd block deleted from the input, then one way could be:

 sed -ne '
    ## Objective: To delete the 3rd-block from input.

    ## Where a block begins with [ on a line by itself
    ## and the block ends with a ] on a separate line.
 

    # skip the non-block lines right away.
    /^\[$/,/^\]$/!bend
 

    # increment the block counter

    x;s/$/./;x
    
    # accumulate the block
    :loop
    $bend;N
    /\n\]$/!bloop
 

    # we now have the whole block in the pattern space.
    # check whether this is the 3rd block?
    x
    /^.\{3\}$/{
 # yes: this needs to be deleted
 :remaining
 n;p
 bremaining
    }
    # no: just display as is
    x
 

    :end
    p
 ' yourfile

 

 Note:
     a ) The :loop label mimics the do-while loop.
     b ) The :remaining label is akin to the while(1) infinite loop.
     c ) The above sed code is written with indents & comments sprinkled in to ease the anxiety of non-sed folks who tend to mistake it for hieroglyphics.
 



[Non-text portions of this message have been removed]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.