Simple Bash Script To Count Characters Of Each Words In A File

This is a simple script to count the number of characters in each words in file. The script will count all the number of characters word by word and display the output (see screenshot attached at bottom)

Create Bash script file
Assume the file's name which you need to count the number of characters is "test" and is located in the "/mnt/" directory. You can edit the the path and file name(red in color) in the script.

[root@server ~]# vi counting-letters-in-a-file

#Bash script for counting the letters in each words of the file.
#put the file named "test" in the current working directory.
for i in $(cat /mnt/test); do
    count=$((count + 1))
    echo "Word $count ($i) contains $(echo -n $i | wc -c) characters"
#Add the above text and Save the file and exit

Set execution permission
[root@server ~]# chmod 755 counting-letters-in-a-file

Run the script
The file name "test" should match in the bash script to get the correct output. You may need to edit the file name "test" and type your own file name and path.
[root@server ~]# ./counting-letters-in-a-file
Sample output screenshot

Was this helpfull ?


Related Posts Plugin for WordPress, Blogger...

Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Bluehost Coupons