[PLUG] Counting Paragraphs in a Text File

Wil Cooley wcooley at nakedape.cc
Wed Sep 7 00:25:36 UTC 2005


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

(See the man page for RS='' instead of RS='\n\n'.)

AIX's grep has a paragraph mode, which is useful not only for text but
also finding relevant stanzas in configuration files and utility output.
With the above technique, you can achieve the same result.

Remember, of course, that "awk '/FOO/'" is basically the same as 'grep
FOO' (the record-print is implicit without another action). So you can
apply the same trick:

awk -v RS='' '/FOO/'

and find paragraphs with "FOO".  Unfortunately, the paragraphs
themselves are broken with this, so you can add them back in with:

awk -v RS='' '/FOO/ {print $0, "\n"}'

Wil
-- 
Wil Cooley <wcooley at nakedape.cc>
Naked Ape Consulting, Ltd. <http://nakedape.cc>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.pdxlinux.org/pipermail/plug/attachments/20050906/d5848f31/attachment.asc>


More information about the PLUG mailing list