Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-12-2010
Registered User
 

Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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:

Code:
======================
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?


Code:
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.
Sponsored Links
    #2  
Old 03-12-2010
thillai_selvan's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 190
Thanks: 0
Thanked 1 Time in 1 Post
You can use the following way.
consider that the file is having the content as follows


Code:
cat file
ERROR
hai
this is test
WARNING


Code:
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 ==

Last edited by thillai_selvan; 03-12-2010 at 07:16 AM..
Sponsored Links
    #3  
Old 03-12-2010
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 8,506
Thanks: 67
Thanked 401 Times in 390 Posts

Code:
grep 'ERROR' file

    #4  
Old 03-12-2010
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 2,295
Thanks: 10
Thanked 127 Times in 98 Posts
Quote:
Originally Posted by lnino View Post

Code:
if [ "$(cat database.txt)" = "ERROR" ];

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:


Code:
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 ;-) ).

bakunin
Sponsored Links
    #5  
Old 03-12-2010
karthigayan's Avatar
Registered User
 

Join Date: Apr 2009
Location: India , Chennai
Posts: 137
Thanks: 0
Thanked 3 Times in 3 Posts
Thumbs up

Try this,


Code:
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

Sponsored Links
    #6  
Old 03-12-2010
murugaperumal's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
You can use the following code


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

Sponsored Links
    #7  
Old 03-12-2010
Registered User
 

Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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. :-)

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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comparing file names to text document gman UNIX for Dummies Questions & Answers 1 01-27-2009 06:27 AM
Help with find and replace w/string containing special characters CAGIRL UNIX for Dummies Questions & Answers 4 10-07-2008 07:13 PM
Mutt - Word Document or Formatted text as a Message krsenkumar UNIX for Advanced & Expert Users 1 04-05-2008 11:10 AM
Looking for command(s)/ script to find a text string within a file wrwelden Shell Programming and Scripting 5 11-22-2006 11:53 PM
Help on find a document merlin UNIX for Dummies Questions & Answers 1 02-14-2002 07:42 PM



All times are GMT -4. The time now is 03:39 AM.