LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 07-08-2015, 10:28 AM   #1
sd1983benson
LQ Newbie
 
Registered: Nov 2010
Location: Saginaw, Michigan
Posts: 1

Rep: Reputation: 0
Need to grep count two phrases in a log file


I need to do a grep count for two strings in the same line. Only need the count for what I am looking at.

Tried: grep -c "Phrase part one" | "phrase part two" *.log

I can do grep -c "Phrase part one" *.log just fine. But need to limit it down to where it has a specific phrase part two because phrase part one could be tied to multiple "types" of phrase part two.

I do not get an error. It just hangs there and I have to break out.

Thanks.
 
Old 07-08-2015, 11:31 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by sd1983benson View Post
I need to do a grep count for two strings in the same line. Only need the count for what I am looking at.

Tried: grep -c "Phrase part one" | "phrase part two" *.log

I can do grep -c "Phrase part one" *.log just fine. But need to limit it down to where it has a specific phrase part two because phrase part one could be tied to multiple "types" of phrase part two.

I do not get an error. It just hangs there and I have to break out.

Thanks.
I am not sure why you need to count. Here's how I would do it:
Code:
egrep 'Phrase part one.*phrase part two|phrase part two.*Phrase part one' phrases.txt 
Phrase part one blah foo bar phrase part two
echo $?
0
As you can see, if grep (or in this case egrep) catches BOTH the phrases on the same line, no matter what phrase appears first, it returns a 0 (success). If you want to, you can expand your script based on the exit code (0) if you need.

Best regards,
HMW

Edit:
The phrases.txt file I tried this on looks like this:
Code:
Phrase part one blah foo bar phrase part two
Phrase part one phrase foobar zap bom bang
phrase part two silmarillion part Phrase part three

Last edited by HMW; 07-08-2015 at 11:38 AM.
 
Old 07-08-2015, 11:48 AM   #3
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
If we want to stick with grep.
Code:
grep "Phrase part one" *.log | grep -c "phrase part two"
 
  


Reply



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
Find & grep - how to return pathes, not grep phrases ? postcd Linux - General 2 11-25-2014 12:43 PM
Batch Log File Count number of "unknown userā? algorithm soph75 Linux - Newbie 2 10-14-2014 04:50 AM
Need to read log file line to certain string then grep out 400 jaa1180 Linux - General 9 06-21-2013 08:57 AM
[SOLVED] log file grep by date and year JJJCR Linux - General 10 01-14-2013 09:24 PM
grep for specific phrases Harry Seldon Linux - Newbie 5 05-27-2008 07:58 PM

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

All times are GMT -5. The time now is 08:10 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