From owner-freebsd-questions@FreeBSD.ORG Fri Jul 12 15:45:47 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 18F0940B for ; Fri, 12 Jul 2013 15:45:47 +0000 (UTC) (envelope-from bonomi@mail.r-bonomi.com) Received: from mail.r-bonomi.com (mx-out.r-bonomi.com [204.87.227.120]) by mx1.freebsd.org (Postfix) with ESMTP id D3A251CFA for ; Fri, 12 Jul 2013 15:45:46 +0000 (UTC) Received: (from bonomi@localhost) by mail.r-bonomi.com (8.14.4/rdb1) id r6CFi7EH003683; Fri, 12 Jul 2013 10:44:07 -0500 (CDT) Date: Fri, 12 Jul 2013 10:44:07 -0500 (CDT) From: Robert Bonomi Message-Id: <201307121544.r6CFi7EH003683@mail.r-bonomi.com> To: guru@unixarea.de Subject: Re: sed Guru wanted In-Reply-To: <20130712090404.GA9263@sh4-5.1blu.de> Cc: freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Jul 2013 15:45:47 -0000 > 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 > 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. 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.