Extracting a variable listing

Robert Bonomi bonomi at mail.r-bonomi.com
Wed Aug 18 17:05:22 UTC 2010


> Date: Wed, 18 Aug 2010 17:14:50 +0200
> From: "Dr. A. Haakh" <bugReporter at 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.



More information about the freebsd-questions mailing list