Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 04-02-2013, 07:11 PM   #1
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
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,344

Rep: Reputation: 1303Reputation: 1303Reputation: 1303Reputation: 1303Reputation: 1303Reputation: 1303Reputation: 1303Reputation: 1303Reputation: 1303Reputation: 1303
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
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 4,140

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

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
LQ Newbie
Registered: Apr 2013
Posts: 2

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

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

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

Rep: Reputation: 231Reputation: 231Reputation: 231
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


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 12:43 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration