Thursday, February 28, 2008

finding a word in a script using unix shell script

Interesting script to count the number of words in a txt



#!/bin/ksh

#finding a word in a script using unix shell script


filename=$$;
read_filename_pattern(){
while [ ! -e $filename ]
do
echo -n 'Enter a filename : '
read filename
done;
echo -n Enter the word you want to search for
read word


}

if [ $# -lt 2 ]
then
echo Useage $0 filename pattern
read_filename_pattern;
else
filename=$1
word=$2;

fi;


wc1=`wc -w $filename |cut -d" " -f1`
wc2=`sed s/$word//g txt|wc -w |cut -d' ' -f1`
echo `expr $wc1 - $wc2`

No comments: