Help answer threads with 0 replies.
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 12-14-2009, 12:14 PM   #1
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Rep: Reputation: 15
logical operators in grep

How can I implement logical operators in grep? I checked the man info and couldn't figure it out.

For example, I'd like to display lines in file1.txt that contains word1 OR word2.

cat file1.txt | grep word1 OR word2

Thanks in advance.
Old 12-14-2009, 01:30 PM   #2
Registered: Jun 2007
Posts: 152

Rep: Reputation: 37
try the --regexp flag.
Old 12-14-2009, 01:43 PM   #3
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927Reputation: 927
grep -E "word1|word2" file1.txt

No need to swing cats =}
1 members found this post helpful.
Old 12-14-2009, 07:55 PM   #4
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
another tool you can use is awk,
awk '/WORD1|WORD2/' file
awk '/WORD1/&&/WORD2/' file
1 members found this post helpful.


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
grep/egrep logical AND function? boozer_2 Linux - Newbie 11 04-10-2010 01:19 AM
how to do Logical AND with grep or egrep amit_pansuria Programming 2 08-09-2009 02:39 AM
Extending logical volume LogVol01 Insufficient allocatable logical exte swap space umeshsharma Linux - Newbie 4 06-22-2009 12:26 PM
PHP5: Logical Operators vs. If-Else unreal128 Programming 2 11-15-2006 10:52 PM
? on logical operators in BASH eroica Linux - Software 10 02-01-2005 01:12 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:59 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
Open Source Consulting | Domain Registration