Shell Script To Count Vowels, Blank Spaces, Characters, Number of line and Symbols

in Categories Academic, Decision Making, String Management last updated February 20, 2009
# Shell program to count
# Vowels, blank spaces, characters, number of line and symbols
# A, E, I, O, U 
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions :
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
if [ $# -ne 1 ]
	echo "$0 fileName"
	exit 1
if [ ! -f $file ]
	echo "$file not a file"
	exit 2
# read vowels
exec 3<&0
while read -n 1 c
  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 )"

Share this on:

12 comment

  1. #!/bin/bash
    ### Script to count vowels, no.of lines,blank spaces, words in a given file
    echo " Enter the file name "
    read a
    char=`cat $a | wc -m`
    echo "The number of characters in file are:" $char
    line=`cat $a | wc -l`
    echo "The number of lines are:" $line
    vow=`cat $a | grep -o "[aAeEiIoOuU]" |wc -l`
    echo "The number of vowels are:" $vow
    space=`cat $a | grep -o " "| wc -l`
    echo "The number of blank spaces are:" $space

  2. i used grep command but it give wrong output .. count vowel and consonant in file
    i think i search only for one time

  3. no need for the loop in counting vowels…

    grep -o “[AEIOUaeiou]” $file|wc -l

    …will do the trick

  4. I need to count number blank records for particular column.

    total record in a file is 100
    total blank in column1 is 40 or so.

    How to get this?

    Thanks in advance

  5. Hello Aswin Triadhi

    you can easily replace the word with sed using the following command

    sed ‘s/stopped/idle/g’
    this will replace the word globally…

  6. Halo viviek

    How to replace a word in file text
    I have printers.conf in /var/cups/printers.conf
    and I want replace the State Stopped to State Idle with shell script

    Info MY_PRINT
    DeviceURI hp:/net/hp_LaserJet_4250?ip=
    State Stopped
    StateTime 1254732779
    Accepting Yes
    Shared Yes
    JobSheets none none
    QuotaPeriod 0
    PageLimit 0
    KLimit 0
    OpPolicy default
    ErrorPolicy stop-printer

    I want replace the State Stopped with State Idle, how ?

    Have a question? Post it on our forum!