1. Overview

Often, we need to manipulate or analyze text data in a Linux environment. One such task in text processing is counting the number of vowels in a given string. This operation is performed for text analytics, data validation, language detection, cryptanalysis, and other reasons.

In this tutorial, we’ll check out the methods to count the number of vowels in a string using the grep, tr, and awk commands, and a for loop.

2. Using the grep Command

In Unix-like operating systems, the grep command is used for searching and matching patterns within text files or data streams. Moreover, we can also utilize it to find the number of vowels in the given string of a Bash script.

Here, we’ll use a chain of commands:

$ echo "Welcome to Baeldung Linux Series" | grep -io '[aeiou]' | wc -l
12

Now, let’s discuss the inner workings of the script:

  • grep counts the number of the vowels in the given string using the [aeiou] pattern
  • with grep, the -i option makes the pattern matching case-insensitive, and -o tells grep to output only matched parts of the string
  • pipe (|) operator redirects the output of grep to the wc command which counts the number of lines in the output via -l

Here, the character group [aeiou] represents a regex pattern that matches any single character that is a vowel (a, e, i, o, or u). It enables grep to find and count all vowels in the sample string.

3. Using a for Loop

In Bash, a for loop is a control structure that repeatedly executes a set of commands for the defined number of iterations over a range of values. It’s primarily used for processing data, iterating over array elements, and performing repetitive tasks.

However, in this scenario, we’ll use a for loop for counting the number of vowels in a string.

First, let’s view the for_vowels.sh script via cat:

$ cat for_vowels.sh
#!/bin/bash

read -p "Enter a string: " input_string

vowel_count=0

for ((i=0; i<${#input_string}; i++)); do
    char="${input_string:i:1}"
    if [[ "$char" =~ [aeiouAEIOU] ]]; then
        ((vowel_count++))
    fi
done

echo "Number of vowels in the string: $vowel_count"

In the script above, we use the read command to get some input into the input_string variable, showing a prompt via -p. After that, we initialize vowel_count to 0. Then, a for loop iterates through each character of the input_string. Within the loop, we check each character char to see if it’s a vowel within the specified if condition. If so, we increment vowel_count.

At the end, the echo command displays the count of vowels as stored in the vowel_count variable.

Now, let’s make the for_vowels.sh script executable using chmod, and run it in the terminal:

$ chmod +x for_vowels.sh
$ ./for_vowels.sh
Enter a string:
Welcome to Baeldung Linux Series
Number of vowels in the string: 12

As a result, the output shows the correct number of vowels in the given string.

4. Using the tr and grep Commands

We can also use the tr and grep commands in a Bash script to get the number of vowels of a specified string.

To do so, let’s use a basic pipeline:

$ echo "Welcome to Baeldung Linux Series" | tr '[:upper:]' '[:lower:]' | tr -cd 'aeiou' | wc -c
12

Now, let’s discuss how the tr and grep commands work to calculate the number of vowels in the sample string:

  • the pipe (|) operator redirects our string to the first tr command
  • tr ‘[:upper:]’ ‘[:lower:]’ converts any uppercase characters to lowercase
  • -cd in the second tr removes all characters except for ‘aeiou’
  • wc counts the characters in the filtered string

As a result, we get the same count from earlier.

5. Using the awk Command

We can use the awk command to define patterns and operations to manipulate and extract information from files and data.

In this case, we’ll use it in our command for counting the number of vowels in a string:

$ echo "Welcome to Baeldung Linux Series" | awk 'BEGIN{IGNORECASE=1} {for (i=1; i<=length; i++) if (substr($0, i, 1) ~ /[aeiou]/) count++} END{print count}'
12

In the above awk command, the BEGIN block sets the IGNORECASE variable to 1, which makes the pattern matching case-insensitive. Then, the for loop iterates through each character of the input string and increments the count variable wherever a vowel is found.

Lastly, the END block prints the final count value, which represents the total number of vowels.

So, we get an equivalent result via awk as well.

6. Conclusion

In this article, we’ve explored several methods to count the number of vowels in a string. These methods included the grep, tr, awk commands, and a for loop.

Naturally, we can use the grep command to perform vowel counting operations quickly and easily. While a for loop is considered a great choice in scenarios when we need to have control and flexibility, it may be less efficient for large inputs. Moreover, we can also utilize the awk command to handle more complex text processing.

In comparison, the combination of tr and grep commands is usually the most effective solution as it ensures accurate vowel counting regardless of letter case and removes unwanted characters efficiently.

Comments are closed on this article!