0

I am trying to write a simple bash script in which it takes in a text file, loops through the file and tells me how many times a certain string appears in the file. I want to eventually use this for a custom log searcher (for instance, search for the words 'log in' in a particular log file, etc.), but am having some difficulty as I am relatively new to bash. I want to be able to quickly search different logs for different terms at my will and see how many times they occur. Everything works perfectly until I get down to my loops. I think that I am using grep wrong, but am unsure if that is the issue. My loop codes may seem a little strange because I have been at it for a while and have been constantly tweaking things. I have done a bunch of searching but I feel like I am the only one who has ever had this issue (hopefully not because it is incredibly simple and I just suck). Any and all help is greatly appreciated, thanks in advance everyone.

edit: I would like to account for every instance of the string and not just one instance per line

#!/bin/bash

echo "This bash script counts the instances of a user-defined string in a file."

echo "Enter a file to search:"

read fileName

echo " "

echo $path

if [ -f "$fileName" ] || [ -d "$fileName" ]; then

echo "File Checker Complete: '$fileName' is a file."
echo " "
echo "Enter a string that you would like to count the occurances of in '$fileName'."

read stringChoice
echo " "
echo "You are looking for '$stringChoice'. Counting...."

#TRYING WITH A WHILE LOOP
count=0
cat $fileName | while read line
do
    if echo $line | grep $stringChoice; then
        count=$[ count + 1 ]
done
echo "Finished processing file"




#TRYING WITH A FOR LOOP
#    count=0
#    for i in $(cat $fileName); do
#        echo $i  
#        if grep "$stringChoice"; then
#            count=$[ $count + 1 ]
#            echo $count
#        fi
#    done

if [ $count == 1 ] ; then
    echo " "
    echo "The string '$stringChoice' occurs $count time in '$fileName'."

elif [ $count > 1 ]; then
    echo " "
    echo "The string '$stringChoice' occurs $count times in '$fileName'."

fi
elif [ ! -f "$fileName" ]; then

echo "File does not exist, please enter the correct file name."

fi

2 Answers 2

3

To find and count all occurrences of a string, you could use grep -o which matches only the word instead of the entire line and pipe the result to wc

read string; grep -o "$string" yourfile.txt | wc -l
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input. I would have preferred the answer to contain a loop, but I appreciate the help. Will note this method of doing so for future projects.
0

You made basic syntax error in the code. Also, the variable of count was never updating as the the while loop was being executed in a subshell and thus the updated count value was never reflecting back.

Please change your code to the following one to get desired result.

#!/bin/bash

echo "This bash script counts the instances of a user-defined string in a file."

echo "Enter a file to search:"

read fileName

echo " "

echo $path

if [ -f "$fileName" ] ; then

        echo "File Checker Complete: '$fileName' is a file."
        echo " "
        echo "Enter a string that you would like to count the occurances of in '$fileName'."

        read stringChoice
        echo " "
        echo "You are looking for '$stringChoice'. Counting...."

#TRYING WITH A WHILE LOOP
        count=0
        while read line
        do
                if echo $line | grep $stringChoice; then
                        count=`expr $count + 1`
                fi
        done < "$fileName"
        echo "Finished processing file"
        echo "The string '$stringChoice' occurs $count time in '$fileName'."

elif [ ! -f "$fileName" ]; then
        echo "File does not exist, please enter the correct file name."
fi

1 Comment

This is PERFECT! Exactly what I was looking for. The help is much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.