Splitting up a too-wide text file

Larry Wall lwall at jpl-devvax.jpl.nasa.gov
Fri Mar 15 12:36:27 AEST 1991


In article <THOMSON.91Mar14072844 at zazen.macc.wisc.edu> thomson at zazen.macc.wisc.edu (Don Thomson) writes:
: I've got a file of ASCII text that has lines that are too long to easily
: print, formatted in columns.  I'd like to run the file through a filter that
: will essentially break each page in half horizontally at a column break and
: place the right-hand side of the broken-off text on a new following page,
: resulting in a new file of reasonable width.  I've got a few relatively
: inelegant solutions in mind, but am interested in suggestions on how other
: people might approach the problem with an appropriate combination of UNIX
: tools.  Any ideas?

The sed/cut/colrm solutions are okay unless you really do want the pages
to alternate, in which case you'll have to program it somehow.  I wouldn't
try to program it in shell, though it's certainly possible.  Here's a crack
at in (of all things) Perl.  :-)

#!/usr/bin/perl

$LINES = 55;			# put 55 lines on a page
$TEMPLATE = 'A80A*';		# split after 80 columns

while (<>) {
    chop;
    ($a,$b) = unpack($TEMPLATE, $_);
    push(@a, $a . "\n");
    push(@b, $b . "\n");
    &dopage if @a >= $LINES;
}
&dopage if @a;

sub dopage {
    print @a, "\f", @b, "\f";
    @a = @b = ();
}

I suppose this could be generalized to print n pages up.

Overkill, eh?

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.questions mailing list