UNIX Programming, Certification, System Administration, Performance Tuning Reference Books
Count Specific Letters and Words

Can anyone give me an idea how to make a shell script that can count the number of sentences, or a specific word eg. 'and' or a specific letter 'e'??? Pls give me and idea. I'm not very expereinced in UNIX yet. Thx.

/-----------------------------/
The following awk script count the number of words WORD in the input_file.
It assumes that the word is delimited by non alphabetic letters.

awk '
{ count += gsub(/(^|[^[:alpha:]])WORD([^[:alpha:]]|$)/,"_x_") }
END { print count }
' input_file

/-----------------------------/
WORD=the
file:
the the the other
then bathe
the the
count=3?

/-----------------------------/
to continue obfuscating:
perl -e '$word=shift;$count=0;while (<>){$count += s/\b$word\b//g;}print "$count\n";' WORD file

/-----------------------------/
Exact....
New version:

awk '
{
??while ((c = sub(/(^|[^[:alpha:]])the([^[:alpha:]]|$)/,"_x_")) > 0)
???? count += c;
}
END { print count }
' input file

Quick Links:
Do you have a UNIX Question?

Unix Home: Unix System Administration Hints and Tips