grep with logic operators
Clash Royale CLAN TAG#URR8PPP
up vote
13
down vote
favorite
Is there a grep-like utility that will enable me to do grep searches with logic operators. I want to be able to nest and combine the logical constructs freely. For example, stuff like this should be possible:
grep (term1 && term2) || (term1 && (term3 xor term4)) *
I realize this can be done with vanilla grep and additional bash scripting, but my goal here is to avoid having to do that.
grep
add a comment |Â
up vote
13
down vote
favorite
Is there a grep-like utility that will enable me to do grep searches with logic operators. I want to be able to nest and combine the logical constructs freely. For example, stuff like this should be possible:
grep (term1 && term2) || (term1 && (term3 xor term4)) *
I realize this can be done with vanilla grep and additional bash scripting, but my goal here is to avoid having to do that.
grep
add a comment |Â
up vote
13
down vote
favorite
up vote
13
down vote
favorite
Is there a grep-like utility that will enable me to do grep searches with logic operators. I want to be able to nest and combine the logical constructs freely. For example, stuff like this should be possible:
grep (term1 && term2) || (term1 && (term3 xor term4)) *
I realize this can be done with vanilla grep and additional bash scripting, but my goal here is to avoid having to do that.
grep
Is there a grep-like utility that will enable me to do grep searches with logic operators. I want to be able to nest and combine the logical constructs freely. For example, stuff like this should be possible:
grep (term1 && term2) || (term1 && (term3 xor term4)) *
I realize this can be done with vanilla grep and additional bash scripting, but my goal here is to avoid having to do that.
grep
grep
edited Jan 5 '15 at 11:53
asked Jan 5 '15 at 11:21
Nikita Kiryanov
75116
75116
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
10
down vote
accepted
With awk
, as with perl
, you'll have to wrap terms in //
, but it can be done:
awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))'
add a comment |Â
up vote
21
down vote
There are lot of ways to use grep
with logical operators.
Use
|
to separate multiple patterns for the OR condition.Example:
grep 'pattern1|pattern2' filename
Use the
-E
option to send multiple patterns for the OR
condition.Example:
grep -E 'pattern1|pattern2' filename
Using a single
-e
matches only one pattern, but using multiple-e
option matches more than one pattern.Example:
grep -e pattern1 -e pattern2 filename
grep -v
can simulate the NOT operation.There is no AND operator in
grep
, but you can brute-force
simulate AND by using the-E
option.Example :
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The above example will match all the lines that contain both
pattern1 and pattern2 in either order.)
1
-E
is not quite equivalent to&&
due to the fact that it is order-sensitive
â iruvar
Jan 5 '15 at 13:58
grep foo | grep bar
is a more general way to do AND.
â Kenster
Jan 5 '15 at 15:43
1
+1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
â Eric Hepperle - CodeSlayer2010
Apr 20 '17 at 15:57
add a comment |Â
up vote
5
down vote
You could use perl:
perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'
add a comment |Â
up vote
0
down vote
sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *
I believe that accomplishes what you're trying to do. It d
eletes from output any line which doesn't match term1
, it b
ranches out of the script (and so autoprints) any line that remains and that matches term2
, and for lines that remain it deletes any which do not match term3
and from those any that do match term4
.
sed
scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.
add a comment |Â
up vote
0
down vote
I use to chain grep
commands to achieve a logical AND:
grep expr1 filename | grep expr2
I believe it is pretty straightforward, Unix-like and elegant.
Then you can combine (as @Tushi thoroughly explained) with the -E
option for OR-ing and -v
for negating.
Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
With awk
, as with perl
, you'll have to wrap terms in //
, but it can be done:
awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))'
add a comment |Â
up vote
10
down vote
accepted
With awk
, as with perl
, you'll have to wrap terms in //
, but it can be done:
awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))'
add a comment |Â
up vote
10
down vote
accepted
up vote
10
down vote
accepted
With awk
, as with perl
, you'll have to wrap terms in //
, but it can be done:
awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))'
With awk
, as with perl
, you'll have to wrap terms in //
, but it can be done:
awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))'
answered Jan 5 '15 at 12:31
muru
33.9k578147
33.9k578147
add a comment |Â
add a comment |Â
up vote
21
down vote
There are lot of ways to use grep
with logical operators.
Use
|
to separate multiple patterns for the OR condition.Example:
grep 'pattern1|pattern2' filename
Use the
-E
option to send multiple patterns for the OR
condition.Example:
grep -E 'pattern1|pattern2' filename
Using a single
-e
matches only one pattern, but using multiple-e
option matches more than one pattern.Example:
grep -e pattern1 -e pattern2 filename
grep -v
can simulate the NOT operation.There is no AND operator in
grep
, but you can brute-force
simulate AND by using the-E
option.Example :
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The above example will match all the lines that contain both
pattern1 and pattern2 in either order.)
1
-E
is not quite equivalent to&&
due to the fact that it is order-sensitive
â iruvar
Jan 5 '15 at 13:58
grep foo | grep bar
is a more general way to do AND.
â Kenster
Jan 5 '15 at 15:43
1
+1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
â Eric Hepperle - CodeSlayer2010
Apr 20 '17 at 15:57
add a comment |Â
up vote
21
down vote
There are lot of ways to use grep
with logical operators.
Use
|
to separate multiple patterns for the OR condition.Example:
grep 'pattern1|pattern2' filename
Use the
-E
option to send multiple patterns for the OR
condition.Example:
grep -E 'pattern1|pattern2' filename
Using a single
-e
matches only one pattern, but using multiple-e
option matches more than one pattern.Example:
grep -e pattern1 -e pattern2 filename
grep -v
can simulate the NOT operation.There is no AND operator in
grep
, but you can brute-force
simulate AND by using the-E
option.Example :
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The above example will match all the lines that contain both
pattern1 and pattern2 in either order.)
1
-E
is not quite equivalent to&&
due to the fact that it is order-sensitive
â iruvar
Jan 5 '15 at 13:58
grep foo | grep bar
is a more general way to do AND.
â Kenster
Jan 5 '15 at 15:43
1
+1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
â Eric Hepperle - CodeSlayer2010
Apr 20 '17 at 15:57
add a comment |Â
up vote
21
down vote
up vote
21
down vote
There are lot of ways to use grep
with logical operators.
Use
|
to separate multiple patterns for the OR condition.Example:
grep 'pattern1|pattern2' filename
Use the
-E
option to send multiple patterns for the OR
condition.Example:
grep -E 'pattern1|pattern2' filename
Using a single
-e
matches only one pattern, but using multiple-e
option matches more than one pattern.Example:
grep -e pattern1 -e pattern2 filename
grep -v
can simulate the NOT operation.There is no AND operator in
grep
, but you can brute-force
simulate AND by using the-E
option.Example :
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The above example will match all the lines that contain both
pattern1 and pattern2 in either order.)
There are lot of ways to use grep
with logical operators.
Use
|
to separate multiple patterns for the OR condition.Example:
grep 'pattern1|pattern2' filename
Use the
-E
option to send multiple patterns for the OR
condition.Example:
grep -E 'pattern1|pattern2' filename
Using a single
-e
matches only one pattern, but using multiple-e
option matches more than one pattern.Example:
grep -e pattern1 -e pattern2 filename
grep -v
can simulate the NOT operation.There is no AND operator in
grep
, but you can brute-force
simulate AND by using the-E
option.Example :
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The above example will match all the lines that contain both
pattern1 and pattern2 in either order.)
edited Dec 15 '17 at 13:54
agc
4,3221935
4,3221935
answered Jan 5 '15 at 12:12
Thushi
6,06621137
6,06621137
1
-E
is not quite equivalent to&&
due to the fact that it is order-sensitive
â iruvar
Jan 5 '15 at 13:58
grep foo | grep bar
is a more general way to do AND.
â Kenster
Jan 5 '15 at 15:43
1
+1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
â Eric Hepperle - CodeSlayer2010
Apr 20 '17 at 15:57
add a comment |Â
1
-E
is not quite equivalent to&&
due to the fact that it is order-sensitive
â iruvar
Jan 5 '15 at 13:58
grep foo | grep bar
is a more general way to do AND.
â Kenster
Jan 5 '15 at 15:43
1
+1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
â Eric Hepperle - CodeSlayer2010
Apr 20 '17 at 15:57
1
1
-E
is not quite equivalent to &&
due to the fact that it is order-sensitiveâ iruvar
Jan 5 '15 at 13:58
-E
is not quite equivalent to &&
due to the fact that it is order-sensitiveâ iruvar
Jan 5 '15 at 13:58
grep foo | grep bar
is a more general way to do AND.â Kenster
Jan 5 '15 at 15:43
grep foo | grep bar
is a more general way to do AND.â Kenster
Jan 5 '15 at 15:43
1
1
+1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
â Eric Hepperle - CodeSlayer2010
Apr 20 '17 at 15:57
+1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
â Eric Hepperle - CodeSlayer2010
Apr 20 '17 at 15:57
add a comment |Â
up vote
5
down vote
You could use perl:
perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'
add a comment |Â
up vote
5
down vote
You could use perl:
perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'
add a comment |Â
up vote
5
down vote
up vote
5
down vote
You could use perl:
perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'
You could use perl:
perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'
edited Jan 5 '15 at 13:26
answered Jan 5 '15 at 11:49
michas
14.6k33569
14.6k33569
add a comment |Â
add a comment |Â
up vote
0
down vote
sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *
I believe that accomplishes what you're trying to do. It d
eletes from output any line which doesn't match term1
, it b
ranches out of the script (and so autoprints) any line that remains and that matches term2
, and for lines that remain it deletes any which do not match term3
and from those any that do match term4
.
sed
scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.
add a comment |Â
up vote
0
down vote
sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *
I believe that accomplishes what you're trying to do. It d
eletes from output any line which doesn't match term1
, it b
ranches out of the script (and so autoprints) any line that remains and that matches term2
, and for lines that remain it deletes any which do not match term3
and from those any that do match term4
.
sed
scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *
I believe that accomplishes what you're trying to do. It d
eletes from output any line which doesn't match term1
, it b
ranches out of the script (and so autoprints) any line that remains and that matches term2
, and for lines that remain it deletes any which do not match term3
and from those any that do match term4
.
sed
scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.
sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *
I believe that accomplishes what you're trying to do. It d
eletes from output any line which doesn't match term1
, it b
ranches out of the script (and so autoprints) any line that remains and that matches term2
, and for lines that remain it deletes any which do not match term3
and from those any that do match term4
.
sed
scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.
answered Jan 5 '15 at 17:24
mikeserv
44.6k565150
44.6k565150
add a comment |Â
add a comment |Â
up vote
0
down vote
I use to chain grep
commands to achieve a logical AND:
grep expr1 filename | grep expr2
I believe it is pretty straightforward, Unix-like and elegant.
Then you can combine (as @Tushi thoroughly explained) with the -E
option for OR-ing and -v
for negating.
Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).
add a comment |Â
up vote
0
down vote
I use to chain grep
commands to achieve a logical AND:
grep expr1 filename | grep expr2
I believe it is pretty straightforward, Unix-like and elegant.
Then you can combine (as @Tushi thoroughly explained) with the -E
option for OR-ing and -v
for negating.
Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I use to chain grep
commands to achieve a logical AND:
grep expr1 filename | grep expr2
I believe it is pretty straightforward, Unix-like and elegant.
Then you can combine (as @Tushi thoroughly explained) with the -E
option for OR-ing and -v
for negating.
Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).
I use to chain grep
commands to achieve a logical AND:
grep expr1 filename | grep expr2
I believe it is pretty straightforward, Unix-like and elegant.
Then you can combine (as @Tushi thoroughly explained) with the -E
option for OR-ing and -v
for negating.
Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).
answered Oct 4 at 8:02
Campa
1033
1033
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f177513%2fgrep-with-logic-operators%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password