LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   logical AND (https://www.linuxquestions.org/questions/programming-9/logical-and-660961/)

tostay2003 08-06-2008 03:37 PM

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"

ta0kira 08-06-2008 03:56 PM

Code:

egrep Word1 file | egrep Word2
ta0kira

edit:
If you need a single statement, try this.
Code:

egrep '(Word1.*Word2|Word2.*Word1)' file

tostay2003 08-06-2008 04:00 PM

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

ta0kira 08-06-2008 04:14 PM

Quote:

Originally Posted by tostay2003 (Post 3239082)
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.

chrism01 08-06-2008 11:38 PM

Try the -P (treat as perl regex) option

ls | grep -P word1.*word2


All times are GMT -5. The time now is 09:10 PM.