LinuxQuestions.org
Visit Jeremy's Blog.
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 04-02-2013, 07:11 PM   #1
blackvelvet
LQ Newbie
 
Registered: Apr 2013
Posts: 2

Rep: Reputation: Disabled
How to add an "OR" to my grep search


So I'm using this grep search to search through this text document...

grep 002 file_name.txt | grep -c 161

Its a grep search that just counts how many times the number "002" and "161" occur on the same line.

It works perfect, but I would like to modify it to search for and count how many times ("002") AND ("161" OR "162" OR "163") occur on the same line. How would I do that?
 
Old 04-02-2013, 08:03 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,285

Rep: Reputation: 1029Reputation: 1029Reputation: 1029Reputation: 1029Reputation: 1029Reputation: 1029Reputation: 1029Reputation: 1029
You need to use extended regex - the manpage explains all; even solving this for you in the general discussion.
awk will do this sort of this in one pass.
 
Old 04-02-2013, 08:12 PM   #3
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,843

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Just use grep's -e option.

Code:
grep 002 file.txt | grep -c -e 161 -e 162 -e 163
This is an AND operation
grep stra | grep strb

This is an OR operation
grep -e stra -e strb

Last edited by suicidaleggroll; 04-02-2013 at 08:13 PM.
 
2 members found this post helpful.
Old 04-02-2013, 08:30 PM   #4
blackvelvet
LQ Newbie
 
Registered: Apr 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank so much!
 
Old 04-05-2013, 09:13 PM   #5
H5X00R
Member
 
Registered: Apr 2013
Posts: 39

Rep: Reputation: 1
You can also use egrep like this

Code:
egrep 'pattern1|pattern2|pattern3' file.txt
The | pipe here works as an OR operator.
 
Old 04-05-2013, 10:55 PM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 778
Blog Entries: 2

Rep: Reputation: 199Reputation: 199
For the special case of matching 161 162 or 163 you can use 16 followed by a character class.
That leaves | alternatives for 002 before or after the other text.

egrep -c '002.*16[123]|16[123].*002' file_name.txt
 
  


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
LXer: Mega-victory: Kim Dotcom search warrants "invalid," mansion raid "illegal" LXer Syndicated Linux News 0 07-11-2012 02:30 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing adamlucansky Linux - General 8 09-25-2009 07:26 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM


All times are GMT -5. The time now is 09:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration