LinuxQuestions.org
Visit Jeremy's Blog.
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, 11: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, 12:31 PM   #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 12:38 PM.
 
Old 07-08-2015, 12:48 PM   #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


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

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

All times are GMT -5. The time now is 09:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration