Date: Fri, 12 Jul 2013 10:44:07 -0500 (CDT) From: Robert Bonomi <bonomi@mail.r-bonomi.com> To: guru@unixarea.de Cc: freebsd-questions@freebsd.org Subject: Re: sed Guru wanted Message-ID: <201307121544.r6CFi7EH003683@mail.r-bonomi.com> In-Reply-To: <20130712090404.GA9263@sh4-5.1blu.de>
next in thread | previous in thread | raw e-mail | index | archive | help
> From owner-freebsd-questions@freebsd.org Fri Jul 12 05:13:11 2013
> Date: Fri, 12 Jul 2013 11:04:04 +0200
> From: Matthias Apitz <guru@unixarea.de>
> To: freebsd-questions@freebsd.org
> Subject: sed Guru wanted
>
>
> Hello,
>
> I can delete in a text file with
>
> sed '/pattern1/,/pattern2/d' < file
>
> all lines between the lines with the given patterns, including themself
> also; how could I specify that the deletion should exclude the line with
> /pattern1/, i.e. the addr is something like /pattern1/+1 ?
IF you use ed(1) style commands, then '/pattern1/+1,/pattern2/d' _is_ the
answer. <GRIN>
See: 'man 1 ed', specifically the LINE ADDRSSING section.
That said, it is also trivial with a simple 'state machine' construct in
awk(1):
/pattern2/ { skipping = 0; next; }
skipping { next; }
/pattern1/ { skipping = 1; next; }
Note: omit the 'next;' from the 'pattern' line if you want that line to be
INCLUDED in the output;
Note: the order of the lines shown is important.
Comment: one _can_ implement something similar to the above state machine
in ed (making use of looping and conditional executin commands, and the
'hold' buffer to track state), but it gets (*very*) messy, difficult to
maintain, and presents a 'decoding' problem for the =next= person who has
to maintain it.
_I_ find awk to be generally preferable (more easily maintainable) for
any situation involving anything more than trivial line range specifications.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201307121544.r6CFi7EH003683>
