[PLUG] Counting Paragraphs in a Text File

Travis Spencer travislspencer at gmail.com
Wed Sep 7 03:19:48 UTC 2005


On 9/6/05, Wil Cooley <wcooley at nakedape.cc> wrote:
> On Tue, 2005-09-06 at 17:01 -0700, Rich Shepard wrote:
> >    'wc' will tell me the number of characters, words, and lines in a text
> > file. Is there a simple utility that will tell me how many paragraphs there
> > are if each paragraph is defined by two newlines?
> 
> Sure; you can change the record separator in Awk and count the records:
> 
> awk -v RS='' 'END { print "Paragraphs:", FNR }' filename.txt

If a paragraph is *exactly* two new line characters in a row, then a
less accurate way that is a little easier on the wrists would be to
just use grep like this: `grep -c ^$'.  For instance:

> man procmailrc | awk -v RS='' 'END { print "Paragraphs:", FNR }'
Paragraphs: 149
> echo -n "Paragraphs: "; man procmailrc | grep -c ^$
Paragraphs: 153

-- 

Regards,

Travis Spencer



More information about the PLUG mailing list