• Arun Kumar

Shell Script to Count Characters

Linux shell script allows user to create a script that read the files and make the report of Vowels, Blank Spaces, Characters, Number of lines, and symbol.


vi count_characters.sh
# !/bin/bash 

file=$1
v=0
 
if [ $# -ne 1 ]
then
	echo "$0 fileName"
	exit 1
fi
if [ ! -f $file ]
then
	echo "$file not a file"
	exit 2
fi
 
# read vowels
exec 3<&0
while read -n 1 c
do 
  l="$(echo $c | tr '[A-Z]' '[a-z]')"
  [ "$l" == "a" -o "$l" == "e" -o "$l" == "i" -o "$l" == "o" -o "$l" == "u" ] && (( v++ )) || :
done < $file
 
echo "Vowels : $v"
echo "Characters : $(cat $file | wc -c)"
echo "Blank lines : $(grep  -c '^$' $file)"
echo "Lines : $(cat $file|wc -l )"

Here is the sample output of the above script

shell script to count characters

More ways to use count in file

  • You can use it without a loop to count vowels.

  • You can also use wc and case statements to execute.

71 views

Recent Posts

See All