Lets build in steps a utility to count the number of occurences of a particular word in text that is piped into it e.g. cat textfile.txt | cwords.sh apple Before that we look at arithmetic expansion, read built-in and the tr command. ============================================================== Command substitution $(commands) Arithmetic expansion - works for integers $(( )) Arithmetic expansion ivar=6 echo $(($ivar + 10)) ============================================================== read built-in read NAME - Reads a single line from stdin and assigns to variable NAME - At the prompt, $ read iword two words $ echo $iword two words ============================================================== tr command Man page says "tr - translate or delete characters" NB: Note the word character above. NB: It does not translate words - it only translates single characters e.g. see second example below. tim@debian-i386:~$ echo "UNIX is simple" | tr "i" "a" UNIX as sample tim@debian-i386:~$ echo "UNIX is simple" | tr "is" "at" UNIX at tample tim@debian-i386:~$ echo "UNIX is simple" | tr "pm" "99" UNIX is si99le tim@debian-i386:~$ echo "UNIX is simple" | tr "is" "aT" UNIX aT Tample tim@debian-i386:~$ echo "UNIX is simple" | tr "a-z" "A-Z" UNIX IS SIMPLE tim@debian-i386:~$ echo "UNIX IS SIMPLE" | tr "A-Z" "a-z" unix is simple tim@debian-i386:~$ echo "UNIX is simple" | tr -d "i" UNIX s smple tim@debian-i386:~$ echo "UNIX is simple" | tr -d "is" UNIX mple tim@debian-i386:~$ echo "UNIX is simple" | tr " " "," UNIX,is,simple ============================================================== ITERATION 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits.txt no snowflake feels guilty in an avalanche politics is the dark shadow cast by business tim@debian-i386:~$ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ cat cwords_1.sh ~~~~~~~~~~~~~~~~~~~~~ #!/bin/bash while read inputline do echo $inputline done ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cat fruits.txt | ./cwords_1.sh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ no snowflake feels guilty in an avalanche politics is the dark shadow cast by business tim@debian-i386:~$ ============================================================== ITERATION 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits.txt no snowflake feels guilty in an avalanche politics is the dark shadow cast by business tim@debian-i386:~$ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ cat cwords_2.sh ~~~~~~~~~~~~~~~~~~~~~~~~ #!/bin/bash while read inputline do for word in $inputline do echo $word done done ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits.txt | ./cwords_2.sh no snowflake feels guilty in an avalanche politics is the dark shadow cast by business ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ============================================================== ITERATION 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits.txt no snowflake feels guilty in an avalanche politics is the dark shadow cast by business tim@debian-i386:~$ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat cwords_3.sh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #!/bin/bash while read inputline do for word in $inputline do if [ $word = $1 ] then echo $word fi done done ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits.txt | ./cwords_3.sh snowflake snowflake tim@debian-i386:~$ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ============================================================== ITERATION 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits2.txt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ no snowflake feels guilty in an avalanche politics is the dark shadow cast by business A week is a long time in politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat cwords_4.sh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #!/bin/bash count=0 while read inputline do for word in $inputline do if [ $word = $1 ] then let count=count+1 # count=$(($count + 1)) fi done done echo $count ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits2.txt | ./cwords_4.sh politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ============================================================== ITERATION 5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits3.txt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ no snowflake feels guilty in an avalanche politics is the dark shadow cast by business POLITICS A week is a long time in politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat cwords_5.sh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #!/bin/bash count=0 while read inputline do inputline=$( echo $inputline | tr "A-Z" "a-z" ) echo $inputline for word in $inputline do if [ $word = $1 ] then let count=count+1 # count=$(($count + 1)) fi done done echo $count ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits3.txt | ./cwords_5.sh politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ no snowflake feels guilty in an avalanche politics is the dark shadow cast by business politics a week is a long time in politics 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ QUESTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ What happens if you run the script with the following input? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits3.txt | ./cwords_5.sh POLITICS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AND What happens if you run the script with the following input? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits3.txt | ./cwords_5.sh politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTPUT - When searching for "POLITICS" and "politics" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits3.txt | ./cwords_5.sh POLITICS no snowflake feels guilty in an avalanche politics is the dark shadow cast by business politics a week is a long time in politics 0 tim@debian-i386:~$ cat fruits3.txt | ./cwords_5.sh politics no snowflake feels guilty in an avalanche politics is the dark shadow cast by business politics a week is a long time in politics 3 tim@debian-i386:~$ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ How would you correct this problem? A. For a solution see ITERATION 6 below. ============================================================== ITERATION 6 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits3.txt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ no snowflake feels guilty in an avalanche politics is the dark shadow cast by business POLITICS A week is a long time in politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat cwords_6.sh #!/bin/bash searchword=$1 count=0 while read inputline do inputline=$( echo $inputline | tr "A-Z" "a-z" ) searchword=$( echo $searchword | tr "A-Z" "a-z") echo $inputline for word in $inputline do if [ $word = $searchword ] then let count=count+1 # count=$(($count + 1)) fi done done echo $count ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTPUT - When searching for "POLITICS" and "politics" - This is now working as it should! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits3.txt | ./cwords_6.sh POLITICS no snowflake feels guilty in an avalanche politics is the dark shadow cast by business politics a week is a long time in politics 3 tim@debian-i386:~$ cat fruits3.txt | ./cwords_6.sh politics no snowflake feels guilty in an avalanche politics is the dark shadow cast by business politics a week is a long time in politics 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ============================================================== NB: We have put in some commas, double quotes and full-stops in the file below: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits4.txt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ no snowflake feels guilty in an avalanche politics, is the dark shadow cast by business "POLITICS" matters A week is a long time in politics. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits4.txt | ./cwords_6.sh politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ no snowflake feels guilty in an avalanche politics, is the dark shadow cast by business "politics" matters a week is a long time in politics. 0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ QUESTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Why are we now getting a 0 in the output above?? How would we solve this problem? A. See ITERATION 7 Below. ============================================================== ITERATION 7 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat cwords_7.sh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #!/bin/bash searchword=$1 count=0 while read inputline do # the double quotes are being escaped in the line below # by preceding them with the \ character. inputline=$( echo $inputline | tr -d ".,()'\"" ) inputline=$( echo $inputline | tr "A-Z" "a-z" ) searchword=$( echo $searchword | tr "A-Z" "a-z") echo $inputline for word in $inputline do if [ $word = $searchword ] then let count=count+1 # count=$(($count + 1)) fi done done echo $count ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits4.txt | ./cwords_6.sh politics no snowflake feels guilty in an avalanche politics, is the dark shadow cast by business "politics" matters a week is a long time in politics. 0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tim@debian-i386:~$ cat fruits4.txt | ./cwords_7.sh politics no snowflake feels guilty in an avalanche politics is the dark shadow cast by business politics matters a week is a long time in politics 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ============================================================== ITERATION 3 - A variation on Iteration 3 (above) In the example below we have used the tr command to translate all lowercase characters to UPPERCASE. To run it, copy and paste the text into it4. Then key in the following: chmod u+x it4 ./it4 JINGLE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #!/bin/bash cat jinglebells.txt | tr "a-z" "A-Z" > jingo.txt while read inputline do for word in $inputline do if [ $word = $1 ] then echo $word fi done done < jingo.txt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Note: Contents of jinglebells.txt is below: Dashing through the snow In a one horse open sleigh O'er the fields we go Laughing all the way Bells on bob tails ring Making spirits bright What fun it is to laugh and sing A sleighing song tonight Oh, jingle bells, jingle bells Jingle all the way Oh, what fun it is to ride In a one horse open sleigh Jingle bells, jingle bells Jingle all the way Oh, what fun it is to ride In a one horse open sleigh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~