Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!ames!ptsfa!ihnp4!ihuxy!lied From: lied@ihuxy.ATT.COM (Bob Lied) Newsgroups: comp.unix.questions Subject: Re: awk or sed question Message-ID: <2020@ihuxy.ATT.COM> Date: Sun, 5-Jul-87 19:40:48 EDT Article-I.D.: ihuxy.2020 Posted: Sun Jul 5 19:40:48 1987 Date-Received: Mon, 6-Jul-87 00:39:00 EDT References: <4780@columbia.UUCP> Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 37 Summary: turn it into an nroff job In article <4780@columbia.UUCP>, agw@broadway.columbia.edu (Art Werschulz) writes: > > Here's the catch: I want to break the long lines only at whitespace. All us well-trained C programmers immediately look for a way to find that last space before column 80. That was my first impulse. Here's an awk script which uses split() in a fairly clever way to try to find the last word before column 80. Besides not handling lines over 160 characters, it also has a fatal flaw if that last word appears earlier in the string. Other than that, it might have at least tutorial value: newform -i file | # Convert those tabs to equivalent spaces! awk 'length > 80 { str = substr($0, 1, 80) n = split(str, words) divide = index(str, words[n]) left = substr($0, 1, divide-1) right = substr($0, divide) print left "\n" right } length <= 80 { print }' Then I had a brainstorm! What well-known text processing program already knows how to break text at white space? Why, of course: nroff (or its much faster local cousin, sroff). Use awk to insert formatter commands, then run the bugger through nroff! (Use grep -v to eliminate the extra blank lines.) newform -i file | awk 'BEGIN {print ".nf" ; print ".na"; print ".nh"; print ".ll 80"} length>80 {print ".fi" ; print ; print ".nf" } length<=80 {print}' | nroff | grep -v '^$' See! All the caffeine and Nutrasweet eventually pays off. Bob Lied ihnp4!ihuxy!lied