Skip to content
Related Articles
Open in App
Not now

Related Articles

Shell Script To Count Number of Words, Characters, White Spaces, and Special Symbols

Improve Article
Save Article
Like Article
  • Last Updated : 20 Apr, 2021
Improve Article
Save Article
Like Article

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:

Input text: GeeksforGeeks are best!

Output :
Number of  Words = 3
Number of  Characters = 24
Number of  White Spaces =2
Number of  Special Symbols = 2

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 {'!','!'}


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"



My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!