#!/usr/bin/perl -n s/[^\w]+/ /g; # replace the non words with space next if(/^\s*$/); # discard sentence with only spaces $totalcount += (split(/ /) - 1); # split the sentence using space and count it END{ print "Total: $totalcount<<\n"; }