Shell Script To Count Number of Words, Characters, White Spaces, and Special Symbols
Last Updated :
23 Jul, 2025
In this article, we are going to see how to count the number of words, characters, whitespace and special symbol in a text file/ input string. Given a text file and tour task is to count the number of words, characters, whitespace and special symbol. So there are many methods and tool that we can use to accomplish our task. For better understanding let's take an example:
Example:
Input text: GeeksforGeeks are best!
Output :
Number of Words = 3
Number of Characters = 24
Number of White Spaces =2
Number of Special Symbols = 2
Explanation:
words are { "GeeksforGeeks", "are", "best!!"}
Characters include number of white spaces(Space),special symbols and letter.
White Spaces are just spaces {' ',' '}
And Special Symbol are {'!','!'}
Approach:
Using wc command. wc command is used to know the number of lines, word count, byte and characters count etc.
- Count the number of words using wc -w. Here, "-w" indicates that we are counting words.
- Count the number of characters using wc -c. Here, "-c" indicates that we are counting each character including white spaces.
- Count the number of white spaces in a string using the following syntax:
Syntax: expr length "$text" - length `echo "$text" | sed "s/ //g"`
Sed command is used to manipulate text, it stands for stream editor. Here, we are using sed to find the whites paces using sed "s/replace this with the whitespace//g". In this syntax sed s refer as substitute and g as globalism and this syntax will search and replace every whitespace in entire text.
Count all the special characters using following regular expression.
Syntax: expr length "${text//[^\~!@#$&*()]/}"
Shell Script:
#! /bin/bash
echo "Enter a String"
# Taking input from user
read text
# Counting words
word=$(echo -n "$text" | wc -w)
# Counting characters
char=$(echo -n "$text" | wc -c)
# Counting Number of white spaces (Here,specificly " ")
# sed "s/ change this to whitespace//g"
space=$(expr length "$text" - length `echo "$text" | sed "s/ //g"`)
# Counting special characters
special=$(expr length "${text//[^\~!@#$&*()]/}")
# Output
echo "Number of Words = $word"
echo "Number of Characters = $char"
echo "Number of White Spaces = $space"
echo "Number of Special symbols = $special"
Output:
Output
Similar Reads
Program to count the number of characters in a word Write a program to count the number of characters in a word. Examples: Input: Word: "programming"Output: 11 Input: Word: "hello"Output: 5 Approach: To solve the problem, follow the below idea: To count the number of characters in a word, maintain a variable to count all the characters. Iterate throu
2 min read
Calculate the number of characters in each word in a Pandas series To calculate the numbers of characters we use Series.str.len(). This function returns the count of the characters in each word in a series. Syntax: Series.str.len() Return type: Series of integer values. NULL values might be present too depending upon caller series. Another way to find the number of
2 min read
Shell Script to Count Lines and Words in a File Linux provides users a great cool feature of the command-line tool along with a graphical user interface where they can perform tasks via ruining command. All of this command returns a status according to their execution. Its execution value can be used for showing errors or take some other action i
6 min read
Program to count the number of words in a sentence Write a program to count the number of words in a given sentence. A word is defined as a sequence of characters separated by spaces. Examples: Input: "The quick brown fox"Output: 4Explanation: The sentence has four words - "The", "quick", "brown", "fox". Input: "The quick brown fox"Output: 4Explanat
3 min read
Program to count the number of consonants in a word Write a program to count the number of consonants in a given word. Consonants are the letters of the English alphabet excluding vowels ('a', 'e', 'i', 'o', 'u'). Examples: Input: "programming"Output: 8 Explanation: The eight consonants are: 'p', 'r', 'g', 'r', 'm', 'm', 'n', 'g') Input: "hello"Outpu
5 min read
Program to count the number of vowels in a word Write a program to count the number of vowels in a given word. Vowels include 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase). The program should output the count of vowels in the word. Examples: Input: "programming"Output: 3Explanation: The word "programming" has three vowels ('o', 'a',
3 min read