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

#!/bin/bash
# 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 : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
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 )"
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
12 comments… add one
  • Sumit Oct 18, 2017 @ 22:39

    #!/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

  • gauri Sep 24, 2017 @ 17:36

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

  • crunk Jul 4, 2015 @ 6:44

    no need for the loop in counting vowels…

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

    …will do the trick

  • Umar Apr 5, 2012 @ 10:23

    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
    Umar

  • ansh Jul 17, 2011 @ 11:11

    why was “exec 3<&0" added to the above script..
    I mean what's its use??

  • Anonymous Jan 28, 2011 @ 5:08

    Hello,
    give me a suggestion to
    print the word containing vowel

  • Suresh Aug 19, 2010 @ 10:38

    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…

  • Aswin Triadhi May 19, 2010 @ 2:36

    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
    example

    Info MY_PRINT
    DeviceURI hp:/net/hp_LaserJet_4250?ip=88.8.8.223
    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 ?
    regards

  • Bran Apr 12, 2010 @ 4:20

    what is 3<&0?

  • osmansaka Dec 21, 2009 @ 14:25

    Man you need to add;
    ı
    ö
    ü
    for vowels too.
    Thank you for script.

  • Jim Charles Feb 20, 2009 @ 16:45

    Vivek, always learn something on your site..thanks

    btw:
    echo “Vowles : $v” (sic) “Vowels”

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.