LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2008, 03:37 PM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
logical AND


How can we try to grep and egrep on two words i.e. both words should exists in the line.

something like
Code:
egrep "Word1&Word2"

Last edited by tostay2003; 08-06-2008 at 03:44 PM.
 
Old 08-06-2008, 03:56 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
egrep Word1 file | egrep Word2
ta0kira

edit:
If you need a single statement, try this.
Code:
egrep '(Word1.*Word2|Word2.*Word1)' file

Last edited by ta0kira; 08-06-2008 at 04:04 PM.
 
Old 08-06-2008, 04:00 PM   #3
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
I mean by using single egrep... looks like logical OR is working
Code:
egrep "Word1|Word2"
, but not logical AND.

Similarly also looking for grep command as well
 
Old 08-06-2008, 04:14 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by tostay2003 View Post
I mean by using single egrep... looks like logical OR is working
Code:
egrep "Word1|Word2"
, but not logical AND.

Similarly also looking for grep command as well
Please see my edit.

grep uses regexes, not logical statements. Regexes give you some logical options, but those options are to express alternatives in a pattern. "And" is implied unless "|" is used, but what you're talking about is parallel analysis rather than a single expression, though it can be made to work in this simple example.
ta0kira

PS Note that "|" itself is "xor", meaning that a position is filled by one or the other, but not both. If you use "*" or "+", you just denote repetition of the previous statement, which is entirely arbitrary.

Last edited by ta0kira; 08-06-2008 at 04:20 PM.
 
Old 08-06-2008, 11:38 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try the -P (treat as perl regex) option

ls | grep -P word1.*word2
 
  


Reply



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
HD logical copy silicon1 Linux - Software 1 09-20-2007 10:28 AM
Primary vs. Logical Jeebizz Slackware 11 09-29-2006 02:13 PM
Logical order nedix_m Linux - Newbie 2 07-24-2006 07:11 AM
Primary or Logical? Brahl Linux - Newbie 1 01-29-2005 09:50 AM
Primary vs Logical djgerbavore Linux - Hardware 2 08-16-2004 06:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration