#! /usr/bin/gawk -f # Last edited on 2016-03-30 22:48:25 by stolfilocal BEGIN { usage = ( ARGV[0] "\\\n" \ " [ -v width=WIDTH ] \\\n" \ " < INPUT.wds > OUTPUT.txt" \ ); # Reads a sequence of words, one or more per line. Packs those words, # separated by spaces, into lines of at most WIDTH columns (default # 72), left-justified. if (width == "") { width = 72; } lin = ""; n = 0; sp = ""; k = 0; } /./ { for (i = 1; i <= NF; i++) { wd = $(i); m = length(wd); if (n + m + k > width) { if (lin != "") { print lin; } lin = wd; n = m; } else { lin = (lin sp wd); n += m + k; } sp = " "; k = 1; # Honor line and paragraph breaks if ((wd == "=") || (wd == "÷")) { print lin; if (wd == "=") { print ""; } lin = ""; n = 0; sp = ""; k = 0; } } } END { if (lin != "") { print lin; } }