LinuxQuestions.org
Register a domain and help support LQ
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 05-06-2009, 02:51 AM   #1
riteshanand40
LQ Newbie
 
Registered: Dec 2007
Posts: 14

Rep: Reputation: 0
Using "OR" type matching pattern with grep command


Hi,

I have a big text file and i want to grep only those lines who either have "word1" or "word2" or "word3".

How can i do it in a single command?
Currently i am doing this as 3 separate commands:
cat filename | grep word1
Cat filename | grep word2
Cat filename | grep word3

Thanks,
Ritesh Anand
 
Old 05-06-2009, 02:59 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,354

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
You don't need the cat - try it like this
Code:
grep -E "word1|word2|word3" filename
or ...
grep -E "word[123]" filename
The latter requires very specific search fields.
 
Old 05-06-2009, 03:44 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You can also use "grep -e word1 -e word2 -e word3".

This is more useful in filtering out unwanted lines from a locate command:
grep -v -e /usr/src -e /mnt/ -e pattern1 -e pattern2
 
Old 05-06-2009, 04:48 AM   #4
riteshanand40
LQ Newbie
 
Registered: Dec 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you very much syg00 and jschiwal.
Although i find the former more easy.
 
  


Reply

Tags
grep


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
"searching for a matching pattern" sharath.bv Programming 6 02-02-2008 11:24 AM
Pattern matching with a "case" statement in BASH King V Programming 8 04-27-2006 04:19 PM
pattern matching question - grep cbriscoejr Programming 1 02-09-2006 08:30 PM
"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:45 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