#!/usr/bin/awk -f function wc(l,w,c) { printf "%7d %7d %7d\n",l,w,c } BEGIN { FS="[[:space:]]+" } $1 { if (lines) wc(lines, words, chars) print $0 chapters++ t_words+=words t_chars+=chars lines=0 words=0 chars=0 } { lines++ for (i=1; i<=NF; i++) if ( $i!="" ) words++ chars+=length+1 } END { t_words+=words t_chars+=chars if (lines) wc(lines, words, chars) printf "%7d ",chapters wc(NR,t_words,t_chars) }