Number of Vowels


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of Vowels
# 1  
Old 09-13-2006
Number of Vowels

Hi Guys,

It may look silly, but I am stuck on it and need some help

How to count number of vowels in a text file

Regards,
Gaurav Goel
# 2  
Old 09-13-2006
Why you need that? is it a school work?
# 3  
Old 09-13-2006
Quote:
Originally Posted by shereenmotor
Why you need that? is it a school work?
i hope the number of posts by me will tell you that i am not in school. Smilie
actually one of my collegue is stuck into a problem and asked for my help.

So, i guess rather than arguing on whether its school work or not, we find the solution to it
# 4  
Old 09-13-2006
Quote:
Originally Posted by gauravgoel
i hope the number of posts by me will tell you that i am not in school. Smilie
actually one of my collegue is stuck into a problem and asked for my help.

So, i guess rather than arguing on whether its school work or not, we find the solution to it
We had post once where an OP's colleague was stuck on a problem which looked like homework. It turned out to be the he was trying to solve his kids schoolwork. And the thread was closed with no further discussion.

Its a judgement call. I am sure you made the best one in this regard.

I will leave the thread open.

Here's one way to do it.

Code:
#! /bin/sh -f

vowels=0

while IFS="\n" read line
do
    length=${#line}
    if [[ $length -eq 1 ]] ; then
    continue
    fi ;
    new_line=$(echo "$line" | tr -d 'aeiouAEIOU')
    new_length=${#new_line}
    vowel=$(($vowel + $length - $new_length))
done < input.txt

echo Vowels=$vowel

I tried it on a few lines from the sh man pages
Code:
[/tmp]$ cat input.txt
       Bash  is  an  sh-compatible  command language interpreter that executes
       commands read from the standard input or from a file.  Bash also incor-
[/tmp]$ ./try.sh 
Vowels=42
[/tmp]$


Last edited by vino; 09-13-2006 at 04:40 AM..
# 5  
Old 09-13-2006
This is one way of achieving the count of vowels in the file 1.txt

Code:
tr '[a-z]' '[A-Z]' < 1.txt | tr -sc 'AEIOU' '[\012*]' | sort | uniq -c

# 6  
Old 09-13-2006
Quote:
Originally Posted by gauravgoel
Hi Guys,

It may look silly, but I am stuck on it and need some help

How to count number of vowels in a text file

Regards,
Gaurav Goel
An alternative in Python, if you have it.

Input.txt:
Quote:
Bash is an sh-compatible command language interpreter that executes
commands read from the standard input or from a file. Bash also incor-
Code:
all = open("input.txt").read()
vowels="aAeEiIoOuU"
for v in vowels:
        total = total + all.count(v) 
 	print "Vowel: ", v, "count: ", all.count(v)
print "Total vowels: " , total

Output:

Code:
Vowel:  a count:  14
Vowel:  A count:  0
Vowel:  e count:  11
Vowel:  E count:  0
Vowel:  i count:  6
Vowel:  I count:  0
Vowel:  o count:  8
Vowel:  O count:  0
Vowel:  u count:  3
Vowel:  U count:  0
Total vowels: 42

# 7  
Old 09-13-2006
hey guys thanks for your responses.
I will be trying these out.

You are right vino, that this is a very intresting problem.
It seemed easy at te first look but once I started thinking about it, it became complex.

I am looking for a single step solution, Charu can you please explain the command

Thanks and Regards,
Gaurav Goel
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Pure C function pointer on printing vowels twice

Have difficulty to understand this pure C code to only print vowels twice from input string. Questions are commented at the end of each place. #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <limits.h> /* *Demonstrate the use of dispatch tables */ /*Print a char... (11 Replies)
Discussion started by: yifangt
11 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

4. UNIX for Dummies Questions & Answers

Grepping Words with No Vowels

Hi! I was trying to grep all the words in a wordlist, (twl), with no vowels. I had a hard time figuring out how to do it, but I finally lit on the -v flag. Here's my question: Why does this work: grep -v '' twl But this doesn't: grep '' twl In the second example, we're asking for lines... (6 Replies)
Discussion started by: sudon't
6 Replies

5. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

6. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

7. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

8. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

9. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies
Login or Register to Ask a Question