Find a special string in a text document

Hi,

i got stuck in creating a search job.

I have a text file which looks like this:

======================
Backup - OK -
Backup - OK -
Backup - ERROR -
Backup - WARNING -
======================

I want to search the text file for the string ERROR.
If this string is present in the text file, there should be an output.

I tried following in the bash, but it won't work. Maybe you have an idea?

if [ "$(cat database.txt)" = "ERROR" ];
then    
 echo "Found an ERROR."
elif [ "$(cat database.txt)" = "WARNING" ];
then
 echo "Found a WARNING".
else
 echo "Backup sucessful."
fi

I get always the output: Backup sucessful, but in the text file the string ERROR is present.

Thanks for your help.

greetz
lino

edit by bakunin: please use code-tags for code-parts or output. Thank you.

You can use the following way.
consider that the file is having the content as follows

cat file
ERROR
hai
this is test
WARNING
while read line
do
if [ "$line" == "ERROR" -o "$line" == "WARNING" ]
then
        echo "Matched line is $line"
fi
done < file

Here I put a sample code for matching a particular word only.
So you need to do some work around.
Instead of using = you have to use ==

grep 'ERROR' file

The problem with this is that you are asking if "cat database.txt" is exactly "ERROR", which of course it isn't. For tasks like yours, where you want to know if a text contains a certain string there is a Unix command named "grep". Have a look at the man page for grep to find out what you could do with it (which is quite a lot, i can assure you).

Your code should look like:

if [ $(grep -c "ERROR" database.txt) = 0 ]; then
     print - "no error found"
else
     print - "error found"
fi

Similar for "WARNING", etc..

I hope this helps (and you don't forget your code-tags from now on :wink: ).

bakunin

Try this,

result=`grep -w "ERROR" database.txt`
err=$?
result=`grep -w "WARNING" database.txt`
war=$?
if [ $err -eq 0 ]
then
 echo "Found an ERROR."
elif [ $war -eq 0 ]
then
 echo "Found a WARNING".
else
 echo "Backup sucessful."
fi

You can use the following code

while read line
do

`echo $line | grep "ERROR" >/dev/null`
if [  $? -eq  0 ];
then
        echo "Matched ERROR the line is $line"
fi
done < file

Hey guys,

thanks a lot for the help.

I will try with method works best in my situation.

From now, I will use code boxes for my code. :slight_smile:

greetz
lnino

---------- Post updated at 10:19 AM ---------- Previous update was at 08:20 AM ----------

Hi.

I have now implemented the code.
I used the code of bakunin.

I am interested in another feature.

Is it possible to count how often the word ERROR has been found in the text file?

Thanks a lot

greetz
lnino

Bakunini's same code:

#!/bin/bash

count=$(grep -c "ERROR" database.txt)
if [ $count = 0 ]; then
     echo "no error found"
else
     echo "$count error(s) found"
fi

grep -c returns the total count of errors.

Hi,

I found a code which counts the words in a text file:

tr -s ' ' '\n' < myfile.txt | grep -c 'searchword'

what is wrong with:

grep -c "ERROR" myfile.txt

How is the output of one vs the other different than expected?

No difference.

Your were just faster with answering.

Thanks.