The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #7 (permalink)  
Old 08-08-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Quote:
Originally Posted by cfajohnson View Post

Code:
tr -cs 'A-Za-z' '\n' < FILE | grep -c "WORD"
This solution does not work.

Here is a sample file:
Code:
a aa aaa
aaa aa a
aaa aa a aaa aa a aaa
Here is one test:
Code:
tr -cs 'A-Za-z' '\n' < FILE | grep -c "aaa"
It gives the total of words as '3', when the answer is '5'.

Here is another possible solution for those who want to use shell script:
Code:
#!/bin/ksh
typeset -i mCnt=0
mWord='aaa'
for mEach in `cat input_file`
do
  if [ "${mEach}" = "${mWord}" ]; then
    mCnt=${mCnt}+1
  fi
done
echo 'Total words for '${mWord}' = '${mCnt}

Last edited by Shell_Life; 08-08-2007 at 08:14 AM..
Reply With Quote