From owner-freebsd-questions@FreeBSD.ORG Wed Aug 18 17:05:22 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C42EA1065694 for ; Wed, 18 Aug 2010 17:05:22 +0000 (UTC) (envelope-from bonomi@mail.r-bonomi.com) Received: from mail.r-bonomi.com (ns2.r-bonomi.com [204.87.227.129]) by mx1.freebsd.org (Postfix) with ESMTP id 98C308FC0C for ; Wed, 18 Aug 2010 17:05:22 +0000 (UTC) Received: (from bonomi@localhost) by mail.r-bonomi.com (8.14.3/rdb1) id o7IH4WWL020512; Wed, 18 Aug 2010 12:04:32 -0500 (CDT) Date: Wed, 18 Aug 2010 12:04:32 -0500 (CDT) From: Robert Bonomi Message-ID: <201008181704.o7IH4WWL020512@mail.r-bonomi.com> To: bugReporter@Haakh.de, jacks@sage-american.com Cc: freebsd-questions@freebsd.org Subject: Re: Extracting a variable listing X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Aug 2010 17:05:22 -0000 > Date: Wed, 18 Aug 2010 17:14:50 +0200 > From: "Dr. A. Haakh" > Subject: Re: Extracting a variable listing > > Jack L. Stone schrieb: > > > > The content I need will always fall beneath a row of pound signs, and there > > is content above that row I don't want, like this: > > > > bunch of rows I don't need here > > ############################### <--- the top of stuff needed > > row1 > > row2 > > row3 > > row4 > > etc, etc.... > > > awk is your friend .-) > this script does exactly what you need > extract.awk > --------------- > /^#####+$/ { > print $0; > getline; > print ">>>>>",$0 > while (match($0, "^[[:print:]]+$")) { > print $0; > getline; > } > } **GAAACK!!!!** let awk do the work for you BEGIN { printing = 0 ; } printing==1 { print $0 ; } /^#####+$/ { printing = 1 ; } usage: awk -f {thatfile} {datafile} >>{masterfile} The BEGIN line is optional, and the '==1' is also un-necessary, but their presecee makes the logic clearer for 'somebody else' that looks at it. One can extract a block of lines from a file with the same logic. just add a pattern with an action of 'printing=0'. If the 'printing=1' line is above the 'print $0' line, the start marker line will be included in the output, if below it won't show. Similarly, if the 'printing=0' line is _below_ the 'print' line, the end marker line will be included, if above it, it won't show. The same basic approach trivially generalizes to extracting multiple blocks with different start/end markers, or to more complex markers -- e.g. trigger criteria that involve multiple lines from the source file.