[PLUG] Counting Paragraphs in a Text File

Travis Spencer travislspencer at gmail.com
Wed Sep 7 17:11:36 UTC 2005


On 9/7/05, Rich Shepard <rshepard at appl-ecosys.com> wrote:
>    Thanks, Travis. I don't understand the difference in the resulting counts,

The difference is that Wil's solution will correctly handle situations
where the number of blank lines between paragraphs is greater than
one.  Also, my quick and dirty solution counts the number of blanks
between paragraphs.  The actual number of paragraphs (even if only one
new line separates them) is one greater than the number of lines
separating them.  For example:

> echo -n "Paragraphs: " ; cat <<EOF | grep -c ^$ 
Test para

Test para 2

Test para 3


Test para 4


Test para 5

Test para 6
EOF
Paragraphs: 7
> cat <<EOF | awk -v RS='' 'END { print "Paragraphs:", FNR }'
Test para

Test para 2

Test para 3


Test para 4


Test para 5

Test para 6
EOF
Paragraphs: 6
> echo -n "Paragraphs: " ; cat <<EOF | grep -c ^$ Test para

Test para 2

Test para 3

Test para 4

Test para 5

Test para 6
EOF
Paragraphs: 5
> cat <<EOF | awk -v RS='' 'END { print "Paragraphs:", FN R }'
Test para

Test para 2

Test para 3

Test para 4

Test para 5

Test para 6
EOF
Paragraphs: 6

> but my immediate need is for a correct number. 

Ya, then Wil hooked ya up.  His solution is a good one.

-- 

Regards,

Travis Spencer



More information about the PLUG mailing list