Wednesday, 6 July 2016

Write a shell script in Linux/Unix that accepts a text file as input and prints the number of words in the file which have at least one vowel as output

#!/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
while read -n 1 c
do
l=$(echo $c | tr [:upper:] [:lower:])
[[ "$l" == "a" || "$l" == "e" || "$l" == "i" || "$l" == "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 )"

OUTPUT:


Print Friendly and PDF

2 comments:

  1. Words with vowel in a file
    v=0;for i in `cat test.txt`;do echo $i|egrep 'a|e|i|o|u' 2> /dev/null && ((v++)); done;echo $v;


    No Vowel words in a file

    v=0;for i in `cat test.txt`;do echo $i|egrep -v 'a|e|i|o|u' 2> /dev/null && ((v++)); done;echo $v;

    ReplyDelete
  2. It's showing syntax error can you please write the correct program

    ReplyDelete

Popular Posts