From lwall@netlabs.com Wed Feb 12 05:37:36 1992
To: Perl-Users@fuggles.acc.Virginia.EDU
From: lwall@netlabs.com (Larry Wall)
Crossposted-To: comp.unix.questions,alt.hackers
Subject: Re: What tool will delete to end of file from a given pattern?
Date: 20 Jan 92 23:57:31 GMT
SUB: Re: What tool will delete to end of file from a given pattern?
SUM: lwall@netlabs.com (Larry Wall)->Perl-Users@fuggles.acc.Virginia.EDU

In article <1992Jan16.154704.17914@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
: From the keyboard of gdridle@pbhya.PacBell.COM (Guy D. Ridley):
: :I want to look through a file until I see 15 consecutive newlines,  and
: :delete them along with the rest of the file.  I've looked at sed but
: :don't see a way for it to count the newlines or to back up once it counts
: :15.  Is this something awk can do?
: 
: If you've seen sed's Towers of Hanoi solution, you might not be 
: so quick to discount it.  But no, counting in sed isn't a lot of 
: fun, nor is handling things that span lines, although this isn't
: generally quite so rough.  Perhaps someone else will be so kind as
: to post a sed solution.  I haven't the stamina for it.

Yeah, it takes so long to type:   :-)

    1{N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;}
    /\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n/{s///; q;}
    N;P;D

[Awkish Perl solutions omitted.]

: This, however, I like much better, since it's just one substitution:
: 
:     perl -0777 -pe 's/\n{16,}[^\0]*$/\n/' 

Your earlier Perl solutions were better in that they didn't slurp the
whole file.  The fastest Perl solution is likely to be something like this:

    perl -e '$/ = "\n" x 16; $_ = <>; s/\n{16}/\n/; print'

: Since I've already give a couple perl solutions, maybe Larry Wall will 
: be inclined to post one in nroff for us.  :-)

Okay.  One moment please...

Put this into /usr/lib/tmac/tmac.asticate, then say "nroff -masticate [files]":

    .pl 1n
    .nf
    .deTR
    .di
    .if \\n(dn>15v .ex
    .nr X \\n(dn-1v
    .if \\nX .nr X -1v
    \\v'\\nXu'\c
    .XX
    .diXX
    .nr c. 0
    .it 1 TR
    ..
    .it 1 TR
    .diXX

Larry Wall
lwall@netlabs.com


