View the Most Wanted LQ Wiki articles.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-06-2009, 03:51 AM   #1
LQ Newbie
Registered: Dec 2007
Posts: 14

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


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

Ritesh Anand
Old 05-06-2009, 03:59 AM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,841

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

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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, 05:48 AM   #4
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.



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 07:58 AM
"searching for a matching pattern" Programming 6 02-02-2008 12:24 PM
Pattern matching with a "case" statement in BASH King V Programming 8 04-27-2006 05:19 PM
pattern matching question - grep cbriscoejr Programming 1 02-09-2006 09:30 PM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM

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