Find a special string in a text document


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a special string in a text document
# 1  
Old 03-12-2010
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.
# 2  
Old 03-12-2010
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 08:16 AM..
# 3  
Old 03-12-2010
Code:
grep 'ERROR' file

# 4  
Old 03-12-2010
Quote:
Originally Posted by lnino
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
MySQL

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
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
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and replace a string in a text file

Dear all, I want to find all the "," in my text file and then replace the commas to a tab. I found a script online but I don't know how to modify the script for my case. Any one can help? Thank you. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set... (2 Replies)
Discussion started by: forevertl
2 Replies

2. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

3. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

4. Shell Programming and Scripting

How to find repeated string in a text file

I have a text file where I need to find the string = ST*850* This string is repetaed several times in the file, so I need to know how many times it appears in the file, this is the text files: ISA*00* *00* *08*925485USNR *ZZ*IMSALADDERSP... (13 Replies)
Discussion started by: cucosss
13 Replies

5. Shell Programming and Scripting

Find string in text file

Hello! Please, help me to write such script. I have some text file with name filename.txt I must check if this file contains string "test-string-first", I must cut from this file string which follows string "keyword-string:" and till first white-space and save it to some variable. For... (3 Replies)
Discussion started by: optik77
3 Replies

6. UNIX for Dummies Questions & Answers

find text enclosed between special characters

Hi, I'm trying to find all DISTINCT words having _mr in the line and ENCLOSED in '/'. For eg below is the text in a file.. /database/new_mr254/1 /database/rawdb/views/new_mr254/1 /database/project/rawdb/tables/new_mr232/1 /database/project/rawdb/views/new_mr253/1... (5 Replies)
Discussion started by: northwest
5 Replies

7. Shell Programming and Scripting

Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name. Following are the find and the replace strings: Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk" Replacestring--self.projpath I tried... (5 Replies)
Discussion started by: noorsam
5 Replies

8. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies

9. Shell Programming and Scripting

Looking for command(s)/ script to find a text string within a file

I need to search through all files with different file suffixes in a directory structure to locate any files containing a specific string (5 Replies)
Discussion started by: wrwelden
5 Replies

10. UNIX for Dummies Questions & Answers

Help on find a document

Hey all, I've been trying to find a document. The document is a list of all supported hardware that Solaris 7 supports. If anyone knows where this can be found thx in advanced. I've tried looking through the docs.sun.com site and can't find what I am after. It's just a list of hardware... (1 Reply)
Discussion started by: merlin
1 Replies
Login or Register to Ask a Question