LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   searching in log files (https://www.linuxquestions.org/questions/linux-newbie-8/searching-in-log-files-4175582769/)

d.standel 06-21-2016 02:06 PM

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.

notKlaatu 06-21-2016 02:11 PM

`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.

syg00 06-21-2016 05:09 PM

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.

JJJCR 06-22-2016 01:54 AM

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

AwesomeMachine 06-22-2016 01:58 AM

$ grep -c debug /var/log/messages

will give the number of lines that contain "debug".

d.standel 06-22-2016 02:57 PM

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.

d.standel 06-22-2016 03:46 PM

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.

keefaz 06-22-2016 04:12 PM

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...)

AwesomeMachine 06-23-2016 12:24 AM

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".


All times are GMT -5. The time now is 06:18 AM.