Need help on a Unix utility to format text.

Robert Felps felps at convex.com
Sat Nov 3 13:51:29 AEST 1990


In <2242 at megadon.UUCP> rer at hpfcdc.fc.hp.com (Rob Robason) writes:
>lp> I need something which will break any word at the 72nd column
>lp> regardless.

>try fold(1).

And if your not on a BSD system or derivative which has fold try the
following awk prog:
-----------------------------------------------------------
#
#########################################################
#
#  This script folds long lines into 60-character lines.
#  A "\e" is appended to indicate that the line was
#  folded, and the residue is then processed.  It is
#  assumed that the input file does not contain tabs.
#  The input line is only broken on spaces.
#
#  To run this script:
#     awk -f fold.script filename
#
#########################################################
#
BEGIN   {
        N = 57  # fold at column 57
}

{       if ((n = length($0)) <= N)
                print                   # fold is unnecessary
        else    {
                split($0,word);
                line = "";
                cnt = 0;
                for (i = 1; length(line) < N; i++)      {
                        line = line " " word[i];
                        cnt = cnt + 1;
                }
                line = line " ";
                if (cnt == NF)
                        printf "%s\n", line
                else    {
                        printf "%s\e\n", line;
                        line = "";
                        for (i = cnt+1; i <= NF; i++)   {
                                line = line " " word[i];
                                cnt = cnt + 1;
                        }
                        printf "%s\n", line
                }
        }
}
-----------------------------------------------------------

You may want to modify it to be more flexible but it does the basic
work already.

enjoy,
Robert Felps                                felps at convex.com
Convex Computer Corp                        OS System Specialist
3000 Waterview Parkway                      Tech. Assistant Ctr
Richardson, Tx.  75083                      1(800) 952-0379



More information about the Comp.unix mailing list