Word Count Blog

July 20, 2009

Word Count in Unix

Filed under: tips and tricks — Tags: , — Thomas Vysokos @ 5:35 pm

All my previous posts were related to the Windows-based word count software, but I thought that it is pretty unfair to forget about millions of UNIX users, so today we have a UNIX word count session.

Of course my UNIX experience is not that huge (it was all about testing a Fedora core under virtual machine), so below you will find mostly the reprints of the word count tips published in other Internet-media.

Word count using the “wc” command (taken from Wiki)

“wc” (short for word count) is a command in Unix-like operating systems. The program reads either standard input or a list of files and generates one or more of the following statistics: number of bytes, number of words, and number of lines (specifically, the number of newline characters). If a list of files is provided, both individual file and total statistics follow.

This is how to use the “wc” command:
• wc -l print the line count
• wc -c print the byte count
• wc -m print the character count
• wc -L print the length of longest line
• wc -w print the word count

Well, taking into consideration the languages that don’t use space mark, byte count will be very much appreciated by users from China, Japan and Thailand.

Word count without using the “wc” command (taken from computing.net)

After user Gburg inquired how to execute a loop to count each word in a file individually, without using the wc command, the following reply followed:

You can use the script below if your words are space seperated.

#!/bin/ksh
typeset -i I=0
{ while read line
do
for wort in `echo $line`
do
I=$I+1
done
done } < $1
echo $I

Many thanks to user Frank from everybody, except users from China, Japan and Thailand, for whom the script there should be another script :-)

That's basically all for UNIX word count today.

P.S. Fans of the open source platforms will definitely like this video. Even such a Windows guy as me liked the trick :-)

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Spam Protection by WP-SpamFree

Powered by WordPress