[TriLUG] AWK for titlecase?

Steve Litt slitt at troubleshooters.com
Sat Mar 15 14:28:53 EDT 2014


On Fri, 14 Mar 2014 21:43:02 -0400
Ken MacKenzie <ken at mack-z.com> wrote:

> Someone wrote it for you:
> 
> http://www.pement.org/awk/titlecase.awk.txt

Thanks Ken. Although the preceding is waaaaay more than I needed, it
inspired me to write this:

=====================================
function titlecase(line, rtrn, fld, ltr){
	for(fld = 1; fld <= NF; fld++){
		ltr = substr($fld, 1, 1)
		sub(/./, toupper(ltr), $fld)
		rtrn = rtrn $fld " "
	}
	sub(/\s+$/, "", rtrn)
	return(rtrn)
}
=====================================

The preceding works to the degree I needed, and I wouldn't have been
able to do it without seeing the URL you referenced, so thank you.

> 
> Are you trying to do one file or a batch of files?

One file at a time. I'm converting tab-indented outlines to
hierarchical HTML, complete with <h?> tags with ids, and body text
identified by lines starting with a colon.

> 
> In the former you could do a regex replace in VIM.

If it were just titlecasing, that's what I'd do. But it's doing a whole
bunch of text processing to convert an outline to HTML. I also could
have written a titlecase filter in whatever language (including Vim),
and piped its results into my AWK converter, but thanks to your help I
found a way to do it with nine lines of AWK.

Thanks,

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance


More information about the TriLUG mailing list