w3resource

Bash Counting Words, Lines, and Characters Exercises & Solutions

1.

Write a Bash script to count the number of lines in a text file named "document.txt".

Code:

#!/bin/bash

# Count the number of lines in the file "document.txt"
line_count=$(wc -l < document.txt)

echo "Number of lines in document.txt: $line_count"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of lines in document.txt: 5

Explanation:

The script uses the "wc" command with the -l option to count the number of lines in the file "document.txt". The result is stored in the variable 'line_count', which is then echoed to the console.

2.

Write a Bash script to count the number of words in a text file named "data.txt".

Code:

#!/bin/bash

# Count the number of words in the file "document.txt"
word_count=$(wc -w < document.txt)

echo "Number of words in document.txt: $word_count"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of words in document.txt: 96

Explanation:

The script utilizes the "wc" command with the -w option to count the number of words in the file "document.txt". The result is stored in the variable 'word_count', which is then echoed to the console.

3.

Write a Bash script to count the number of characters in a text file named "document.txt".

Code:

#!/bin/bash

# Count the number of characters in the file "document.txt"
char_count=$(wc -m < document.txt)

echo "Number of characters in document.txt: $char_count"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of characters in document.txt: 628

Explanation:

In the exercise above,

  • The script uses the "wc" command with the -m option to count the number of characters in the file "document.txt".
  • The result is stored in the variable 'char_count'.
  • Finally, the script echoes the number of characters to the console.

4.

Write a Bash script to count the number of lines in a text file excluding empty lines.

Code:

#!/bin/bash

# Count the number of non-empty lines in the file "document.txt"
line_count=$(grep -v '^$' document.txt | wc -l)

echo "Number of non-empty lines in document.txt: $line_count"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of non-empty lines in document.txt: 5

Explanation:

In the exercise above,

  • The script uses "grep" to filter out empty lines (^$ matches empty lines) from the file "document.txt".
  • Then, it uses wc -l to count the number of lines in the filtered output.
  • The result is stored in the variable 'line_count'.
  • Finally, the script echoes the number of non-empty lines to the console.

5.

Write a Bash script to count the number of words in a text file excluding specific words (e.g., "the", "and").

Code:

#!/bin/bash

# Define the text file
file="document.txt"

# Define the words to exclude
exclude_words=("the" "and")

# Count the number of words in the file excluding specific words
word_count=$(grep -oE '\b\w+\b' "$file" | grep -vFw -f <(printf "%s\n" "${exclude_words[@]}") | wc -w)

echo "Number of words in $file excluding 'the' and 'and': $word_count"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of words in document.txt excluding 'the' and 'and': 92

Explanation:

In the exercise above,

  • The script first defines the text file to be analyzed and the words to be excluded.
  • It then uses grep -oE '\b\w+\b' "$file" to extract all words from the file.
    • \b\w+\b is a regular expression that matches whole words.
    • -o flag makes grep print only the matched parts of the line.
  • The output is then piped to grep -vFw -f <(printf "%s\n" "${exclude_words[@]}").
    • -v inverts the match, so it excludes lines that match any of the specified words.
    • -F treats patterns as fixed strings (not regular expressions).
    • -w matches whole words only.
    • -f <(printf "%s\n" "${exclude_words[@]}") reads the words to be excluded from the process substitution.
  • Finally, the word count is obtained using wc -w, and the result is stored in the variable.

6.

Write a Bash script to count the number of characters in a text file excluding spaces and special characters.

Code:

#!/bin/bash

# Define the text file
file="document.txt"

# Count the number of characters in the file excluding spaces and special characters
char_count=$(grep -oE '[[:alnum:]]' "$file" | wc -l)

echo "Number of characters in $file excluding spaces and special characters: $char_count"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of characters in document.txt excluding spaces and special characters: 503

Explanation:

In the exercise above,

  • The script defines the text file to be analyzed.
  • It then uses grep -oE '[[:alnum:]]' "$file" to extract all alphanumeric characters from the file.
    • [[:alnum:]] is a character class that matches any alphanumeric character.
    • -o flag makes "grep" print only the matched parts of the line.
  • The output is piped to wc -l to count the number of lines, which gives the count of characters.
  • The result is stored in the variable 'char_count', and then printed.

7.

Write a Bash script to count the number of lines containing a specific word (e.g., "shell") in a text file.

Code:

#!/bin/bash

# Define the text file and the word to search for
file="document.txt"
word="shell"

# Count the number of lines containing the word
line_count=$(grep -c "$word" "$file")

echo "Number of lines containing the word '$word' in $file: $line_count"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of lines containing the word 'shell' in document.txt: 3

Explanation:

In the exercise above,

  • The script defines the text file to be analyzed (file) and the word to search for (word).
  • It then uses grep -c "$word" "$file" to count the number of lines containing the specified word in the file.
    • -c flag makes "grep" print only the count of matching lines.
  • The result is stored in the variable 'line_count', and then printed.

8.

Write a Bash script to count the number of occurrences of a specific word (e.g., "is") in a text file.

Code:

#!/bin/bash

# Define the text file and the word to search for
file="document.txt"
word="is"

# Count the number of occurrences of the word
occurrences=$(grep -o "\b$word\b" "$file" | wc -l)

echo "Number of occurrences of the word '$word' in $file: $occurrences"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of occurrences of the word 'is' in document.txt: 2

Explanation:

In the exercise above,

  • The script defines the text file to be analyzed (file) and the word to search for (word).
  • It uses grep -o "\b$word\b" "$file" to search for the word and print only the matching occurrences.
    • -o flag makes "grep" print only the matching parts of the lines.
    • \b denotes word boundaries, ensuring that only whole words are matched.
  • wc -l is used to count the number of lines containing the word.
  • The result is stored in the variable 'occurrences', and then printed.

9.

Write a Bash script to count the number of characters in the longest line of a text file.

Code:

#!/bin/bash

# Define the text file
file="document.txt"

# Find the length of the longest line
max_length=$(awk '{ if (length > max) max = length } END { print max }' "$file")

echo "Length of the longest line in $file: $max_length characters"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Length of the longest line in document.txt: 191 characters

Explanation:

In the exercise above,

  • The script defines the text file to be analyzed (file).
  • It uses "awk" to find the length of the longest line in the file.
    • awk '{ if (length > max) max = length } END { print max }' "$file":
      • For each line of the file, "awk" compares the length of the line with the current maximum length ('max').
      • If the length of the current line is greater than 'max', it updates 'max' to the length of the current line.
      • After processing all lines, "awk" prints the maximum length.
  • The result is stored in the variable 'max_length', and then printed.

10.

Write a Bash script to count the number of unique words in a text file.

Code:

#!/bin/bash

# Define the text file
file="document.txt"

# Count the number of unique words
unique_words=$(tr -s '[:space:]' '\n' < "$file" | tr '[:upper:]' '[:lower:]' | sort -u | wc -l)

echo "Number of unique words in $file: $unique_words"

Output:

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Number of unique words in document.txt: 73

Explanation:

In the exercise above,

  • The script defines the text file to be analyzed (file).
  • It uses tr to split the text file into words, then converts all words to lowercase.
  • tr -s '[:space:]' '\n' < "$file": Translates all whitespace characters into newline characters, effectively splitting the text into words.
  • tr '[:upper:]' '[:lower:]': Converts all words to lowercase to ensure case-insensitive matching.
  • sort -u: Sorts the words alphabetically and removes duplicates, leaving only unique words.
  • wc -l: Counts the number of lines, which corresponds to the number of unique words.
  • The result is stored in the variable 'unique_words' and then printed.

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/bash-script-exercises/counting-words-lines-and-chatacters.php