#!/usr/bin/perl -w

use strict;

if( (@ARGV != 1 && @ARGV != 2) || $ARGV[0] !~ /^\d+$/ ) {
    print <<EOF;
usage: paragraph.pl <index> [ <file> ]
Extract <index>th paragraph from plain text <file> or stdin.  Paragraphs are
blocks of text lines containing more than just white space, separated by one or
multiple lines that contain only white space.  <index> is 0-based.  White-space
lines before the first paragraph are ignored.
EOF
    exit;
}

my $selected= shift @ARGV;

my $currind= 0;

while( <> ) {
    last unless /^\s*$/;
}
print if $selected == 0 && defined;

while( <> ) {
    next if /^\s*$/;
    if( $currind == $selected ) {
        print;
        while( <> ) {
            last if /^\s*$/;
            print;
        }
        exit;
    }
    else {
        while( <> ) {
            last if /^\s*$/;
        }
        ++$currind;
    }
}


