uttam
uttam

Reputation: 75

Count no of character, word and line in a file

I have written this code for char, word, line count but I am not getting expected result. Can anyone help?

#!/bin/bash
echo "enter filename"  

read filename  

count=0  
file=$filename  
file="$(<$file)"  

character="${#file}"   

for ((i=0;i<character;i++))    

do  

echo "${file:i:1}"  

(( count++ ))  

        done  

        echo "No of character =$count"  

Upvotes: 0

Views: 31

Answers (1)

Vishnu T S
Vishnu T S

Reputation: 3914

Use this script

 echo Enter filename
    read file
    w=`cat $file | wc -w`
    c=`cat $file | wc -c`
    l=`grep -c "." $file`
    echo Number of characters in $file is $c
    echo Number of words in $file is $w
    echo Number of lines in $file is $l

Upvotes: 1

Related Questions