sed Guru wanted
Robert Bonomi
bonomi at mail.r-bonomi.com
Fri Jul 12 15:45:47 UTC 2013
> From owner-freebsd-questions at freebsd.org Fri Jul 12 05:13:11 2013
> Date: Fri, 12 Jul 2013 11:04:04 +0200
> From: Matthias Apitz <guru at unixarea.de>
> To: freebsd-questions at 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.
More information about the freebsd-questions
mailing list