[comp.unix.questions] line length

mkb@ohsu-hcx.UUCP (Marilyn Bushway) (05/05/89)

If I have output of 132 characters and I want it to be 80 characters
is there a unix command to change the line length???
nosun!ohsu-hcx!mkb
or 
tektronix!ohsu-hcx!mkb
or nosun!ohsu-hcx!mkb@rutgers.edu
Thanks in advance
Marilyn

-- 
Marilyn Bushway  3181 S.W. Sam Jackson Park Rd. Portland, OR  97201
(503) 279-8328   e-mail  {nosun,tektronix,ogccse,qiclab} ohsu-hcx!mkb

jik@athena.mit.edu (Jonathan I. Kamens) (05/08/89)

In article <160@ohsu-hcx.UUCP> mkb@ohsu-hcx.UUCP (Marilyn Bushway) writes:
>If I have output of 132 characters and I want it to be 80 characters
>is there a unix command to change the line length???

See the man page for fold.  By default it wraps at 80 columns, so you
could just do

	fold <filename>

to wrap a file, or

	<program name> | fold

to wrap the output of a program.

Jonathan Kamens			              USnail:
MIT Project Athena				410 Memorial Drive, No. 223F
jik@Athena.MIT.EDU				Cambridge, MA 02139-4318
Office: 617-253-4261			      Home: 617-225-8218

raulin@tdl.UUCP (Raulin Olivera) (05/09/89)

In article <160@ohsu-hcx.UUCP>, mkb@ohsu-hcx.UUCP (Marilyn Bushway) writes:
> If I have output of 132 characters and I want it to be 80 characters
> is there a unix command to change the line length???
> nosun!ohsu-hcx!mkb
> or 
> tektronix!ohsu-hcx!mkb
> or nosun!ohsu-hcx!mkb@rutgers.edu
> Thanks in advance
> Marilyn
> 
> -- 
> Marilyn Bushway  3181 S.W. Sam Jackson Park Rd. Portland, OR  97201
> (503) 279-8328   e-mail  {nosun,tektronix,ogccse,qiclab} ohsu-hcx!mkb


I would probalbly handle it with and AWK script depending on the size 
of the file.  The AWK script could be run within your shell script.
An example might be:

:
Shell commands ...

awk '
{
    printf("%s\n%s\n", substr($0,1,80), substr($0,81,))
}' inputfile
<EOF>

If you need speed I would do it in C.  An example might be

#define MAXLEN 133

main(argc, argv)
int argc;
char *argv[];
{
    int i, len;
    char line[MAXLEN];

    FILE *fp;

    fp = fopen(argv[1],"r");	/* should add diagnostics here */

    while(fgets(line, LENGTH, fp) != NULL)
    {
	len = strlen(line);
	for(i = 0; i < len; i++)
	{
	    putchar(line[i]);
	    if(i == 79)		/* Carriage return after 80th char */
		putchar('\n');
	}
	putchar('\n');
    }
}


This code should put the first 80 chars on one line and the rest of
the chars on the line following.  This is pretty basic and there are
probably more efficient ways to do it but it should work.  I didn't
test this code but I think it's complete.

		=Ralo->

jik@athena.mit.edu (Jonathan I. Kamens) (05/10/89)

In article <127@tdl.UUCP> raulin@tdl.UUCP (Raulin Olivera) writes:
>In article <160@ohsu-hcx.UUCP>, mkb@ohsu-hcx.UUCP (Marilyn Bushway) writes:
>> If I have output of 132 characters and I want it to be 80 characters
>> is there a unix command to change the line length???
>I would probalbly handle it with and AWK script depending on the size 
>of the file.  The AWK script could be run within your shell script.
>An example might be:
> [ example of doing it using awk ]
>If you need speed I would do it in C.  An example might be
> [ example of doing it using C ]

Can you say, "Reinvent the wheel?"

The shar file below says it all, I think.  I wouldn't normally post
source code for a Berkeley program in comp.unix.questions when it can
be gotten from ftp and uucp archive sites, but it's so short, what the
hell :-)

Isn't Berkeley Unix a wonderful thing?

Jonathan Kamens			              USnail:
MIT Project Athena				410 Memorial Drive, No. 223F
jik@Athena.MIT.EDU				Cambridge, MA 02139-4318
Office: 617-253-4261			      Home: 617-225-8218

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  fold.1 fold.c
# Wrapped by jik@pit-manager on Wed May 10 08:46:00 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'fold.1' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'fold.1'\"
else
echo shar: Extracting \"'fold.1'\" \(823 characters\)
sed "s/^X//" >'fold.1' <<'END_OF_FILE'
X.\" Copyright (c) 1980 Regents of the University of California.
X.\" All rights reserved.  The Berkeley software License Agreement
X.\" specifies the terms and conditions for redistribution.
X.\"
X.\"	@(#)fold.1	6.1 (Berkeley) 4/29/85
X.\"
X.TH FOLD 1 "April 29, 1985"
X.UC
X.SH NAME
Xfold \- fold long lines for finite width output device
X.SH SYNOPSIS
X.B fold
X[
X\-width
X] [
Xfile ...
X]
X.SH DESCRIPTION
X.I Fold
Xis a filter which will fold the contents of the specified files,
Xor the standard input if no files are specified,
Xbreaking the lines to have maximum width
X.I width.
XThe default for
X.I width
Xis 80.
X.I Width
Xshould be a multiple of 8 if tabs are present, or the tabs should
Xbe expanded using
X.IR expand (1)
Xbefore coming to
X.I fold.
X.SH SEE\ ALSO
Xexpand(1)
X.SH BUGS
XIf underlining is present it may be messed up by folding.
END_OF_FILE
if test 823 -ne `wc -c <'fold.1'`; then
    echo shar: \"'fold.1'\" unpacked with wrong size!
fi
# end of 'fold.1'
fi
if test -f 'fold.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'fold.c'\"
else
echo shar: Extracting \"'fold.c'\" \(2311 characters\)
sed "s/^X//" >'fold.c' <<'END_OF_FILE'
X/*
X * Copyright (c) 1980 Regents of the University of California.
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms are permitted
X * provided that the above copyright notice and this paragraph are
X * duplicated in all such forms and that any documentation,
X * advertising materials, and other materials related to such
X * distribution and use acknowledge that the software was developed
X * by the University of California, Berkeley.  The name of the
X * University may not be used to endorse or promote products derived
X * from this software without specific prior written permission.
X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
X */
X
X#ifndef lint
Xchar copyright[] =
X"@(#) Copyright (c) 1980 Regents of the University of California.\n\
X All rights reserved.\n";
X#endif /* not lint */
X
X#ifndef lint
Xstatic char sccsid[] = "@(#)fold.c	5.2 (Berkeley) 6/29/88";
X#endif /* not lint */
X
X#include <stdio.h>
X/*
X * fold - fold long lines for finite output devices
X *
X * Bill Joy UCB June 28, 1977
X */
X
Xint	fold =  80;
X
Xmain(argc, argv)
X	int argc;
X	char *argv[];
X{
X	register c;
X	FILE *f;
X
X	argc--, argv++;
X	if (argc > 0 && argv[0][0] == '-') {
X		fold = 0;
X		argv[0]++;
X		while (*argv[0] >= '0' && *argv[0] <= '9')
X			fold *= 10, fold += *argv[0]++ - '0';
X		if (*argv[0]) {
X			printf("Bad number for fold\n");
X			exit(1);
X		}
X		argc--, argv++;
X	}
X	do {
X		if (argc > 0) {
X			if (freopen(argv[0], "r", stdin) == NULL) {
X				perror(argv[0]);
X				exit(1);
X			}
X			argc--, argv++;
X		}
X		for (;;) {
X			c = getc(stdin);
X			if (c == -1)
X				break;
X			putch(c);
X		}
X	} while (argc > 0);
X	exit(0);
X}
X
Xint	col;
X
Xputch(c)
X	register c;
X{
X	register ncol;
X
X	switch (c) {
X		case '\n':
X			ncol = 0;
X			break;
X		case '\t':
X			ncol = (col + 8) &~ 7;
X			break;
X		case '\b':
X			ncol = col ? col - 1 : 0;
X			break;
X		case '\r':
X			ncol = 0;
X			break;
X		default:
X			ncol = col + 1;
X	}
X	if (ncol > fold)
X		putchar('\n'), col = 0;
X	putchar(c);
X	switch (c) {
X		case '\n':
X			col = 0;
X			break;
X		case '\t':
X			col += 8;
X			col &= ~7;
X			break;
X		case '\b':
X			if (col)
X				col--;
X			break;
X		case '\r':
X			col = 0;
X			break;
X		default:
X			col++;
X			break;
X	}
X}
END_OF_FILE
if test 2311 -ne `wc -c <'fold.c'`; then
    echo shar: \"'fold.c'\" unpacked with wrong size!
fi
# end of 'fold.c'
fi
echo shar: End of shell archive.
exit 0

mchinni@pica.army.mil (Michael J. Chinni, SMCAR-CCS-E) (05/10/89)

In article <160@ohsu-hcx.UUCP>, mkb@ohsu-hcx.UUCP (Marilyn Bushway) writes:
> If I have output of 132 characters and I want it to be 80 characters
> is there a unix command to change the line length???
> nosun!ohsu-hcx!mkb
> or 
> tektronix!ohsu-hcx!mkb
> or nosun!ohsu-hcx!mkb@rutgers.edu
> Thanks in advance
> Marilyn
> 
> -- 
> Marilyn Bushway  3181 S.W. Sam Jackson Park Rd. Portland, OR  97201
> (503) 279-8328   e-mail  {nosun,tektronix,ogccse,qiclab} ohsu-hcx!mkb

On all UNIX systems I have seen, there is a command called "fold" which will do
this for you (assuming you want each 132char. line to become a 80char. line and
a 52 char. line).  If you just want to throw away the extra 52 characters, try
"cut -c1-80".

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
			    Michael J. Chinni
      Chief Scientist, Simulation Techniques and Workplace Automation Team
	 US Army Armament Research, Development, and Engineering Center
 User to skeleton sitting at cobweb    () Picatinny Arsenal, New Jersey  
    and dust covered workstation       () ARPA: mchinni@pica.army.mil
      "System been down long?"         () UUCP: ...!uunet!pica.army.mil!mchinni
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

tale@pawl.rpi.edu (David C Lawrence) (05/10/89)

In <19546@adm.BRL.MIL> mchinni@pica.army.mil (Michael J. Chinni) writes:
MJC> On all UNIX systems I have seen, there is a command called "fold"
MJC> which will do this for you (assuming you want each 132char. line
MJC> to become a 80char. line and a 52 char. line).  If you just want
MJC> to throw away the extra 52 characters, try "cut -c1-80".

Better yet, use colrm.  When you want to just take out columns from a
line, "colrm START [&optional END]" is faster that cut; the difference
is especially noticeable in loops.  The only thing I use cut for is
field operations.

Dave
--
      tale@rpitsmts.bitnet, tale%mts@itsgw.rpi.edu, tale@pawl.rpi.edu