|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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 < fileHere 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
|
|||
|
|||
|
Code:
grep 'ERROR' file |
|
#4
|
|||
|
|||
|
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"
fiSimilar for "WARNING", etc.. I hope this helps (and you don't forget your code-tags from now on ;-) ). bakunin |
|
#5
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| 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 |