Sunday, 17 June 2012

Write a Shell script, which counts the number of words in a file, without taking into consideration the blank space, tab spaces and the newline characters without using wc


read file
l=`wc -l $file|cut -d " " -f 1`
echo $l
count=0
coch=0
for (( i=1; i<=l; i++))
do
        n=1
        line=`head -$i $file|tail -1`
        echo $line
        ch=`echo $line|cut -c $n`
        echo character is $ch
        while [ "$ch" != "" ]
        do
                if [ "$ch" = " " ]
                then
                count=`expr $count + 1`
                else
                coch=`expr $coch + 1`
                fi
                n=`expr $n + 1`
                ch=`echo $line|cut -c $n`

        done
done
echo no. of space $count
echo no. of characters $coch


No comments:

Post a Comment