Date: Wed, 18 Aug 2010 12:04:32 -0500 (CDT) From: Robert Bonomi <bonomi@mail.r-bonomi.com> To: bugReporter@Haakh.de, jacks@sage-american.com Cc: freebsd-questions@freebsd.org Subject: Re: Extracting a variable listing Message-ID: <201008181704.o7IH4WWL020512@mail.r-bonomi.com>
next in thread | raw e-mail | index | archive | help
> Date: Wed, 18 Aug 2010 17:14:50 +0200 > From: "Dr. A. Haakh" <bugReporter@Haakh.de> > 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.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201008181704.o7IH4WWL020512>