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
#!/bin/bash
#Bash script for counting the letters in each words of the file.
#put the file named "test" in the current working directory.
count=0
for i in $(cat /mnt/test); do
count=$((count + 1))
echo "Word $count ($i) contains $(echo -n $i | wc -c) characters"
done
#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
0 comments:
Post a Comment