Operating System - Linux
1753988 Members
6158 Online
108811 Solutions
New Discussion юеВ

how to grep an error out of most current log file

 
Debbie Smith
Advisor

how to grep an error out of most current log file

Hi,
I need to grep for an error out of the most current log file. The log file does roll at a set size so I could have several log files with the same date but different names (ie file.log and file.log01)

Thanks in advance,
Debbie
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: how to grep an error out of most current log file

Hi Debbie:

The simplest approach is something like this, assuming the you were looking for a string that matches "Error 99", for example:

# grep -i "Error 99" log*

The 'grep -i' ignores case. The double quotes around the string keeps the shell from doing anything but passing the whole string to 'grep'. The asterisk (star) followling the file causes the shell to expand *all* filenames so if you have "log" and "log.old", etc. you will search all of them at once. 'grep' will preface its output with the *name* of the file when passed multiple filenames so there is no abmiguity as to what file(s) contain a match.

You can also use regular expressions with 'grep' to better isolate your strings. For instance, "^Error" says to look *only* at the beginning of each line for a string of characters beginning with "Error".

Regards!

...JRF...
Debbie Smith
Advisor

Re: how to grep an error out of most current log file

Thanks James,
The only problem with that is it gives me all the logs with that error and I am only wanting the current log. I have a server with 30+ logs and it is difficult to find out which log is most current that has that error.
James R. Ferguson
Acclaimed Contributor

Re: how to grep an error out of most current log file

Hi Debbie:

OK, if you have a directory in which your log files reside try this:

# cd dir
# F=`ls -1 -t|awk 'NR==1'`;grep -i "Error" $F

This captures the most recently modified file (by name) in F and then 'grep's for your string in that file.

Note that the 'ls -1' is a numeric one to limit columnar output to one file per line.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to grep an error out of most current log file

Hi (again):

...and to show the file that the above command found, add /dev/null thusly:

# cd dir
# F=`ls -1 -t|awk 'NR==1'`;grep -i "Error" $F /dev/null

Regards!

...JRF...
Vibhor Kumar Agarwal
Esteemed Contributor

Re: how to grep an error out of most current log file

ls -ltr /dir
will give you the most current log.

Simply suffix it with your grep.
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: how to grep an error out of most current log file

Use this:

#!/bin/ksh
#script.ksh
cd /directory
for file in `find . -type f -name "file.log*" -exec ls -l {} \; | sort -rk 6,8 | awk '{ print $9 }'`
do
grep -q 'a' $file
if [[ $? -eq 0 ]]
then
echo "$file contents string"
exit 0
fi
done
# end #

Execute it.

hth.
Easy to suggest when don't know about the problem!
Daavid Turnbull
Frequent Advisor

Re: how to grep an error out of most current log file

Too simple ;-)

ls -rt | tail -1 | xargs grep "pattern"

List in reverse date order, grab the last one and grep for your pattern.

If you don't want to use xargs here is a back tick version:

grep "pattern" `ls -rt | tail -1`
Behold the turtle for he makes not progress unless he pokes his head out.