Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
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: 182
Thanks: 0
Thanked 0 Times in 0 Posts
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..
  #3  
Old 03-12-2010
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 6,729
Thanks: 0
Thanked 53 Times in 50 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: 1,856
Thanks: 4
Thanked 9 Times in 9 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
  #5  
Old 03-12-2010
karthigayan's Avatar
Registered User
 

Join Date: Apr 2009
Location: /usr/bin
Posts: 116
Thanks: 0
Thanked 2 Times in 2 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

  #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

  #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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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 07:37 AM.