line length

Raulin Olivera raulin at tdl.UUCP
Wed May 10 01:10:15 AEST 1989


In article <160 at ohsu-hcx.UUCP>, mkb at 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 at 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->



More information about the Comp.unix.questions mailing list