| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
On Thu, 26 Feb 2015 12:12:24 +0200, "Lars Noodén [email protected]
[sed-users]" <[email protected]> wrote:
> Using GNU sed 4.2.2-4ubuntu1 on Ubuntu 14.04 LTS GNU/Linux
>
>
> I have some blocks of text spread over multiple lines and delimited by [
> and ]. ( See the tail of the message for an example. ) I'd like to
> delete the nth [ ] block. The following pattern will delete all [ ]
> blocks:
>
>
> sed '/^\[$/,/^\]/d' in > out
>
>
> That deletes more than I want deleted. How can I make a sed formula or
> two that only deletes the nth block and leaves the others?
>
>
> Regards,
> /Lars
>
>
> ###
>
>
> foo
> [
> aaa
> bbb
> ccc
> ]
> [
> ccc
> bbb
> aaa
> ]
> [
> bbb
> ccc
> aaa
> ]
> [
> bbb
> aaa
> ccc
> ]
> bar
With some assumptions (the main one being that a block does not contain a
lone ]), you can slurp the whole file and use the normal s///n syntax:
:loop
$!{
N
b loop
}
s/\[[^]]*\]//3
change the 3 on the last line to the value you want. As with almost any
solution, that may or may not be appropriate for your actual data and use
case.
--
D.