- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- how to grep an error out of most current log file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 10:57 AM
тАО09-21-2005 10:57 AM
how to grep an error out of most current log file
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
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 11:07 AM
тАО09-21-2005 11:07 AM
Re: how to grep an error out of most current log file
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 11:43 AM
тАО09-21-2005 11:43 AM
Re: how to grep an error out of most current log file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 12:11 PM
тАО09-21-2005 12:11 PM
Re: how to grep an error out of most current log file
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 12:41 PM
тАО09-21-2005 12:41 PM
Re: how to grep an error out of most current log file
...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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 04:14 PM
тАО09-21-2005 04:14 PM
Re: how to grep an error out of most current log file
will give you the most current log.
Simply suffix it with your grep.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2005 05:20 PM
тАО09-21-2005 05:20 PM
Re: how to grep an error out of most current log file
#!/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.
- Tags:
- find
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-25-2005 10:41 AM
тАО09-25-2005 10:41 AM
Re: how to grep an error out of most current log file
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`
- Tags:
- tail