Skip to main content
added 174 characters in body
Source Link
#!/bin/bash file=`cat $1` length=`cat $1 | wc -m` // Count the charater count=0 for ((i = 0 ;i < $length;i++)) do //loop to the end of the string if [ "${file:$i:1}" == "$2" ] //Look only character by character (select some characters of the String, (:$i:1) is the range of the choosen characters then count=$((count + 1)) fi done echo $count //test.txt = "Is there a neater way?" #./CountChar test.txt " " >>> 4 

it's an "other" way..x), hope you like it !

#!/bin/bash file=`cat $1` length=`cat $1 | wc -m` count=0 for ((i = 0 ;i < $length;i++)) do if [ "${file:$i:1}" == "$2" ] then count=$((count + 1)) fi done echo $count //test.txt = "Is there a neater way?" #./CountChar test.txt " " >>> 4 

it's an "other" way..x), hope you like it !

#!/bin/bash file=`cat $1` length=`cat $1 | wc -m` // Count the charater count=0 for ((i = 0 ;i < $length;i++)) do //loop to the end of the string if [ "${file:$i:1}" == "$2" ] //Look only character by character (select some characters of the String, (:$i:1) is the range of the choosen characters then count=$((count + 1)) fi done echo $count //test.txt = "Is there a neater way?" #./CountChar test.txt " " >>> 4 

it's an "other" way..x), hope you like it !

Source Link

#!/bin/bash file=`cat $1` length=`cat $1 | wc -m` count=0 for ((i = 0 ;i < $length;i++)) do if [ "${file:$i:1}" == "$2" ] then count=$((count + 1)) fi done echo $count //test.txt = "Is there a neater way?" #./CountChar test.txt " " >>> 4 

it's an "other" way..x), hope you like it !