I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command.
grep -f <textfile> <filetobegrepped>
I'm doing this thru a script where I have a loop which will get the file name to be grepped and search for the messages. If ound I need to display the file which contains the pattern.
During exeuction I have the error as follows:
Usage: grep -hblcnsviw pattern file
Can somebody help what could be wrong here..
This is the piece of code
for FILE in *; do
if [ -d $FILE ]
then
echo "This is a directory. Search not required"
else
grep -f messages.txt $FILE
if [ $? = 0 ]
then
echo "Please check $FILE for errors" >> errorrep
fi
fi
done
grep does not check directories for patterns, only files. It does only descent and do recursive searches when using -R, which you don't do anyway in your example.
So you can spare out all those if/then/else/fi tests.
Also you don't have to check all files inside a for loop since grep does this on it's own when you specify a wildcard as parameter.
To see the name of the file that contains the pattern you got in your list, you can add a -l to grep.
So try something like this while standing in the directory where the supposed files are - else add some path to the wildcard accordingly:
This code is working fine in my system ,
hopefully some unexpected thing would be in the message.txt ,
can u give some idea about the file messages.txt , can u paste some lines of messages.txt
here