Tom Says: Code something crazy every day you feel like it!
I think if you write long programs in awk, you're missing the point, but small programs are great!
#!/bin/sh
# f - format text into 72-char lines
# AFAIK, written by Brian Kernighan
awk '
/./ { for (i = 1; i <= NF; i++) addword($i) }
/^$/ { printline(); print "" }
END { printline() }
function addword(w) {
if(length(line) + length(w) > 72)
printline()
line = line space w
space = " "
}
function printline() {
if(length(line) > 0)
print line
line = space = ""
}
' "$@"
Here is my own personal contribution, closely modeled after the above, and which I used to filter this post, just 'cause.
#!/bin/sh
# para - add paragraph tags to text
awk '
/^<[^\/]/ { html += 1; }
/./ { line(); if(last_line) print last_line; last_line = $0 }
/^$/ { newpara(); last_line = $0; print "" }
END { newpara() }
/^<\// { html -= 1; }
function newpara() {
printf("%s", last_line);
if(!html && inpara) print "</p>";
else print "";
inpara = 0;
}
function line() {
if(!html && !inpara) {
printf("<p>");
inpara = 1;
}
}
' "$@"
Posted Feb 19, 2008, in the late, late night.