LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-23-2016, 08:42 AM   #1
ampapa
LQ Newbie
 
Registered: Feb 2012
Posts: 28

Rep: Reputation: Disabled
grep for ERROR in a folder full of logs...


I have a folder full of logs that I would like to grep for the word 'ERROR' in. Is it possible to get output of the line containing the word along with the log that the word is in?

I'm a bit of a novice in Linux and just trying to get some help with the abundance of logs and how to manage them and review for errors, etc. Possibly get some notification when the word 'ERROR' comes up in a log...

Thanks for any suggestions.

ampapa,
 
Old 09-23-2016, 09:03 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,977

Rep: Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281
Have you read the manpage ?.
 
Old 09-23-2016, 10:29 AM   #3
ampapa
LQ Newbie
 
Registered: Feb 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Yes, somewhat but I guess I was looking for more insight into how others address logging and anticipating errors or system problems in more of a real time situation.

grep -l ERROR * , will list the log files with the ERROR but not the specific lines containing the errors. How would I be able to get a real time list if an error were occurring...

ampapa,
 
Old 09-23-2016, 12:02 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
basic shotgun grep
Code:
grep -w <whole_word> /path/to/search/* -R
excess output, you say?
Show just the files where hits occur:
Code:
grep -w <whole_word> /path/to/search/* -Rl
Level Up:
Code:
find /path/to/search -type f -exec grep -w <whole_word> {} \;
Have fun.
 
Old 09-23-2016, 12:22 PM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,809

Rep: Reputation: 4869Reputation: 4869Reputation: 4869Reputation: 4869Reputation: 4869Reputation: 4869Reputation: 4869Reputation: 4869Reputation: 4869Reputation: 4869Reputation: 4869
Quote:
Originally Posted by ampapa View Post
grep -l ERROR * , will list the log files with the ERROR but not the specific lines containing the errors. How would I be able to get a real time list if an error were occurring...
These are two different things

Code:
grep -n -H <pattern> -R <dir>
can be used the find lines containing the pattern.
But real time list is another issue, cannot be solved just with a grep. And I think you do not really want that.
 
Old 09-23-2016, 01:04 PM   #6
ampapa
LQ Newbie
 
Registered: Feb 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Thanks.

I was looking at LogStash as a more robust option, anyone have any experience with it or can make another open source suggestion?
 
Old 09-30-2016, 12:23 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,782

Rep: Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538
If you wanted to grab an error msg as it occurred, you'd want to prefix the above cmds like 'tail -f <file> |grep ...' .
I've used that interactively.

If you want a programmatic soln I've successfully used Perl with http://search.cpan.org/~mgrabnar/File-Tail-1.3/Tail.pm
 
Old 09-30-2016, 12:46 AM   #8
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
Try this:

Code:
grep -n ERROR $(grep -l ERROR *)

Last edited by c0wb0y; 09-30-2016 at 12:47 AM. Reason: added text
 
1 members found this post helpful.
Old 10-21-2016, 10:51 AM   #9
ampapa
LQ Newbie
 
Registered: Feb 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Habitual,

This gets me closer to a solution that would work for me.

Code:
grep -w <whole_word> /path/to/search/* -Rl
Is it also possible to only search .log files and list line with the error?

Ampapa,
 
Old 10-21-2016, 11:13 AM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by ampapa View Post
Habitual,

This gets me closer to a solution that would work for me.

Code:
grep -w <whole_word> /path/to/search/* -Rl
Is it also possible to only search .log files and list line with the error?

Ampapa,
Sure, try
Code:
grep -w <whole_word> /path/to/search/*.log -Rln
Have fun!
Using the find utility is the next logical step...
Code:
find <path> -type f -iname '*.log' -exec grep <stuff>
 
Old 10-21-2016, 11:15 AM   #11
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654Reputation: 1654
Something like:

Code:
grep -nH <whole_word> /path/to/search/*.log -Rl
 
Old 10-21-2016, 01:28 PM   #12
ampapa
LQ Newbie
 
Registered: Feb 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
Sure, try
Code:
grep -w <whole_word> /path/to/search/*.log -Rln
[/CODE]
Thanks, that got me close enough...

Quote:
-R, -r, --recursive equivalent to --directories=recurse
--include=FILE_PATTERN search only files that match FILE_PATTERN
This worked...

Code:
grep -w <whole_word> /path/to/search/* --include=.log -Rln
Last follow up.. is it possible to also search by line date/time in the file? It would be nice to hone in on current ERROR's...
 
Old 10-21-2016, 01:44 PM   #13
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by ampapa View Post
Thanks, that got me close enough...



This worked...

Code:
grep -w <whole_word> /path/to/search/* --include=.log -Rln
Last follow up.. is it possible to also search by line date/time in the file? It would be nice to hone in on current ERROR's...
Yes it is possible.
See https://www.digitalocean.com/communi...-text-in-linux

and ask if you have any further Qs, and show some of your work.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CentOS7 error logs / system logs AdultFoundry Linux - Newbie 6 06-22-2016 07:10 AM
in need of a script to grep a string in logs - then email cmartz Linux - Server 8 01-24-2013 06:18 AM
Why won't grep work on my logs? Rob00 Linux - Newbie 11 11-04-2009 06:16 PM
Which files contains a full logs of system starting Up... deepclutch Linux - Software 2 12-17-2005 04:28 PM
Which files contains a full logs of system starting Up... deepclutch Linux - Software 6 12-17-2005 02:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration