LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2016, 02:06 PM   #1
d.standel
LQ Newbie
 
Registered: Jun 2016
Posts: 6

Rep: Reputation: Disabled
searching in log files


Hello,
First of all, as I am new to Linux, I would like to thank all of you who helped me with my last question.

Today I am playing around with regular expressions and log files. What I'd like to be able to do is search a log file and display the number of lines that contain DEBUG issues (for example)?

I have tried using
cat [filename] | wc -l grep -i debug

this gives me errors and says -i is not used with wc (I thought I was using it with grep).

Thanks for any assistance you can give.
 
Old 06-21-2016, 02:11 PM   #2
notKlaatu
Senior Member
 
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,077

Rep: Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732
`wc` is a word counting command (words, lines, characters). Why are you using it here at all? Are you trying to also get the total number of words in a log file?

Also, you don't need to `cat` a file to `grep` it. That's like opening a JPG in GIMP, and then going to File > Open and opening that same file in GIMP again. It's already open, you don't have to open it again.

Try something like:

Code:
grep -i debug /path/to/log/file
Not sure what you are trying to do with `wc`, but maybe you are trying to count the number of times 'debug' occurs? In that case, you can count the output of `grep`:

Code:
grep -i debug /path/to/log/file | wc -l

Try running those command separately; first do just the grep, and then the grep and wc. It will make more sense that way: the first just greps the file for the 'debug' string, while the second detects each grep "hit" and adds it to the count, providing you with the summary at the end.

Last edited by notKlaatu; 06-21-2016 at 02:14 PM. Reason: added a wc example
 
1 members found this post helpful.
Old 06-21-2016, 05:09 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Just as you have piped the output of "cat" (unnecessary in this case as implied above) to "wc-l", you would need to pipe the output of "wc -l" to grep. It will not do as you apparently expect. But try it anyway and see if you an figure out why.
grep has a "-c" parameter you might find of interest.
 
Old 06-22-2016, 01:54 AM   #4
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,148

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Cool

Try this:

Quote:
grep -o "debug" /home/bugsbunny/error_put.txt | wc -l
It will count the occurrences for the string "debug" in all the lines.

man grep --> https://www.gnu.org/software/grep/manual/grep.html

Check out this link also: http://quickbytesstuff.blogspot.sg/2...currences.html

Last edited by JJJCR; 06-22-2016 at 02:12 AM. Reason: edit
 
Old 06-22-2016, 01:58 AM   #5
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
$ grep -c debug /var/log/messages

will give the number of lines that contain "debug".
 
1 members found this post helpful.
Old 06-22-2016, 02:57 PM   #6
d.standel
LQ Newbie
 
Registered: Jun 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks.
I was indeed trying to utilize the wc to figure out the number of line in the file that contain the word debug.

I'm new to linux so I was using the cat to get into the file to read it.

As a follow on question, I noticed a large number of debug issues and now I would like to narrow the search. Perhaps is there a way to use a logical AND to find the number lines to contain both debug AND "symbolic link" (say i want to isolate the number of debug issues that are related to symbolic links.

How about a logical OR? say i want to find the number of lines that contain either the word bytes OR links.
 
Old 06-22-2016, 03:46 PM   #7
d.standel
LQ Newbie
 
Registered: Jun 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks.
I was indeed trying to utilize the wc to figure out the number of line in the file that contain the word debug.

I'm new to linux so I was using the cat to get into the file to read it.

As a follow on question, I noticed a large number of debug issues and now I would like to narrow the search. Perhaps is there a way to use a logical AND to find the number lines to contain both debug AND "symbolic link" (say i want to isolate the number of debug issues that are related to symbolic links.

How about a logical OR? say i want to find the number of lines that contain either the word bytes OR links.
 
Old 06-22-2016, 04:12 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Actually OR is simpler:
Code:
# lines that contain word1 OR word2 (maybe both, or just one or another)
grep 'word1\|word2' files...
Both words:
Code:
# lines that contain word1 AND word2 (require both)
grep 'word1.*word2\|word2.*word1' files...
(word1, word2 can be simple character, number, word...)

Last edited by keefaz; 06-22-2016 at 04:14 PM.
 
Old 06-23-2016, 12:24 AM   #9
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
I use piped grep because it's easy to remember:

$ grep -i debug | grep snd-hd

will output every line with both "debug", either upper or lower case, and "snd-hd". I believe the "|" within the regular expression means, "or".
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File searching in log Buhemba Linux - Newbie 4 06-24-2015 01:02 PM
Searching var/log/updates.log using grep noobee26 Linux - Newbie 15 06-27-2011 01:42 PM
convert screen.log and putty.log files into linux readable files aksharb Linux - Software 1 03-20-2011 07:16 AM
searching log files in linux ramanika Linux - Newbie 2 10-21-2008 11:56 AM
Can log files be time stamped? (such as FTP login and transfer log files) bripage Linux - Networking 6 08-08-2002 10:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:51 AM.

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