how can I find all lines containing two words?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I need to check if two words are exist on any line in a text file. there are no limits for the characters of the words For example:
I want to find lines of a text that contain the two words cat and elephant together:
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environment
cats are friendly
In the previous examples, how can I find the lines containing both words
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
I tried grep and awk with no hope, the problem is there are words have upper and lower case so how can I match for both words regardless of its letter status!?
bash
New contributor
add a comment |Â
up vote
2
down vote
favorite
I need to check if two words are exist on any line in a text file. there are no limits for the characters of the words For example:
I want to find lines of a text that contain the two words cat and elephant together:
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environment
cats are friendly
In the previous examples, how can I find the lines containing both words
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
I tried grep and awk with no hope, the problem is there are words have upper and lower case so how can I match for both words regardless of its letter status!?
bash
New contributor
1
Try withgrep
again, but usegrep -i
. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
â Kusalananda
3 hours ago
Related: grep with logic operators
â steeldriver
1 hour ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I need to check if two words are exist on any line in a text file. there are no limits for the characters of the words For example:
I want to find lines of a text that contain the two words cat and elephant together:
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environment
cats are friendly
In the previous examples, how can I find the lines containing both words
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
I tried grep and awk with no hope, the problem is there are words have upper and lower case so how can I match for both words regardless of its letter status!?
bash
New contributor
I need to check if two words are exist on any line in a text file. there are no limits for the characters of the words For example:
I want to find lines of a text that contain the two words cat and elephant together:
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environment
cats are friendly
In the previous examples, how can I find the lines containing both words
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
I tried grep and awk with no hope, the problem is there are words have upper and lower case so how can I match for both words regardless of its letter status!?
bash
bash
New contributor
New contributor
edited 10 mins ago
New contributor
asked 3 hours ago
gormet
164
164
New contributor
New contributor
1
Try withgrep
again, but usegrep -i
. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
â Kusalananda
3 hours ago
Related: grep with logic operators
â steeldriver
1 hour ago
add a comment |Â
1
Try withgrep
again, but usegrep -i
. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?
â Kusalananda
3 hours ago
Related: grep with logic operators
â steeldriver
1 hour ago
1
1
Try with
grep
again, but use grep -i
. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?â Kusalananda
3 hours ago
Try with
grep
again, but use grep -i
. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?â Kusalananda
3 hours ago
Related: grep with logic operators
â steeldriver
1 hour ago
Related: grep with logic operators
â steeldriver
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
With grep
grep -i "cat" file | grep -i "elephant"
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
The flag in grep
is to ignore case (upper/lower)
-i, --ignore-case ignore case distinctions
or awk
awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file
Also, awk
statement can be run as follows:
awk '/cat/&&/elephant/' IGNORECASE=1 file
Theprint $0
block is optional since it is the default action.
â glenn jackman
16 mins ago
@glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
â Goro
14 mins ago
I would leave your answer as it is. FYI the awk command can be "golfed" toawk '/cat/&&/elephant/' IGNORECASE=1 file
-- also I believe IGNORECASE is specific to GNU awk
â glenn jackman
13 mins ago
Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
â Goro
12 mins ago
add a comment |Â
up vote
1
down vote
with GNU sed:
sed -n '/cat/I /elephant/I p' file
or perl
perl -ne 'print if /cat/i and /elephant/i' file
or a single grep
grep -i -e 'cat.*elephant' -e 'elephant.*cat' file
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With grep
grep -i "cat" file | grep -i "elephant"
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
The flag in grep
is to ignore case (upper/lower)
-i, --ignore-case ignore case distinctions
or awk
awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file
Also, awk
statement can be run as follows:
awk '/cat/&&/elephant/' IGNORECASE=1 file
Theprint $0
block is optional since it is the default action.
â glenn jackman
16 mins ago
@glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
â Goro
14 mins ago
I would leave your answer as it is. FYI the awk command can be "golfed" toawk '/cat/&&/elephant/' IGNORECASE=1 file
-- also I believe IGNORECASE is specific to GNU awk
â glenn jackman
13 mins ago
Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
â Goro
12 mins ago
add a comment |Â
up vote
3
down vote
accepted
With grep
grep -i "cat" file | grep -i "elephant"
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
The flag in grep
is to ignore case (upper/lower)
-i, --ignore-case ignore case distinctions
or awk
awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file
Also, awk
statement can be run as follows:
awk '/cat/&&/elephant/' IGNORECASE=1 file
Theprint $0
block is optional since it is the default action.
â glenn jackman
16 mins ago
@glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
â Goro
14 mins ago
I would leave your answer as it is. FYI the awk command can be "golfed" toawk '/cat/&&/elephant/' IGNORECASE=1 file
-- also I believe IGNORECASE is specific to GNU awk
â glenn jackman
13 mins ago
Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
â Goro
12 mins ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With grep
grep -i "cat" file | grep -i "elephant"
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
The flag in grep
is to ignore case (upper/lower)
-i, --ignore-case ignore case distinctions
or awk
awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file
Also, awk
statement can be run as follows:
awk '/cat/&&/elephant/' IGNORECASE=1 file
With grep
grep -i "cat" file | grep -i "elephant"
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
The flag in grep
is to ignore case (upper/lower)
-i, --ignore-case ignore case distinctions
or awk
awk 'BEGINIGNORECASE=1 /cat/&&/elephant/print $0' file
Also, awk
statement can be run as follows:
awk '/cat/&&/elephant/' IGNORECASE=1 file
edited 11 mins ago
answered 3 hours ago
Goro
9,86364689
9,86364689
Theprint $0
block is optional since it is the default action.
â glenn jackman
16 mins ago
@glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
â Goro
14 mins ago
I would leave your answer as it is. FYI the awk command can be "golfed" toawk '/cat/&&/elephant/' IGNORECASE=1 file
-- also I believe IGNORECASE is specific to GNU awk
â glenn jackman
13 mins ago
Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
â Goro
12 mins ago
add a comment |Â
Theprint $0
block is optional since it is the default action.
â glenn jackman
16 mins ago
@glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
â Goro
14 mins ago
I would leave your answer as it is. FYI the awk command can be "golfed" toawk '/cat/&&/elephant/' IGNORECASE=1 file
-- also I believe IGNORECASE is specific to GNU awk
â glenn jackman
13 mins ago
Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
â Goro
12 mins ago
The
print $0
block is optional since it is the default action.â glenn jackman
16 mins ago
The
print $0
block is optional since it is the default action.â glenn jackman
16 mins ago
@glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
â Goro
14 mins ago
@glennjackman thank you so much for the valuable comment. Would suggest adding a note or just removing it entirely?
â Goro
14 mins ago
I would leave your answer as it is. FYI the awk command can be "golfed" to
awk '/cat/&&/elephant/' IGNORECASE=1 file
-- also I believe IGNORECASE is specific to GNU awkâ glenn jackman
13 mins ago
I would leave your answer as it is. FYI the awk command can be "golfed" to
awk '/cat/&&/elephant/' IGNORECASE=1 file
-- also I believe IGNORECASE is specific to GNU awkâ glenn jackman
13 mins ago
Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
â Goro
12 mins ago
Perfect! I will add both.... you keep teaching me new things many thanks! ;-)
â Goro
12 mins ago
add a comment |Â
up vote
1
down vote
with GNU sed:
sed -n '/cat/I /elephant/I p' file
or perl
perl -ne 'print if /cat/i and /elephant/i' file
or a single grep
grep -i -e 'cat.*elephant' -e 'elephant.*cat' file
add a comment |Â
up vote
1
down vote
with GNU sed:
sed -n '/cat/I /elephant/I p' file
or perl
perl -ne 'print if /cat/i and /elephant/i' file
or a single grep
grep -i -e 'cat.*elephant' -e 'elephant.*cat' file
add a comment |Â
up vote
1
down vote
up vote
1
down vote
with GNU sed:
sed -n '/cat/I /elephant/I p' file
or perl
perl -ne 'print if /cat/i and /elephant/i' file
or a single grep
grep -i -e 'cat.*elephant' -e 'elephant.*cat' file
with GNU sed:
sed -n '/cat/I /elephant/I p' file
or perl
perl -ne 'print if /cat/i and /elephant/i' file
or a single grep
grep -i -e 'cat.*elephant' -e 'elephant.*cat' file
answered 15 mins ago
glenn jackman
49.1k467106
49.1k467106
add a comment |Â
add a comment |Â
gormet is a new contributor. Be nice, and check out our Code of Conduct.
gormet is a new contributor. Be nice, and check out our Code of Conduct.
gormet is a new contributor. Be nice, and check out our Code of Conduct.
gormet is a new contributor. Be nice, and check out our Code of Conduct.
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%2f475908%2fhow-can-i-find-all-lines-containing-two-words%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
1
Try with
grep
again, but usegrep -i
. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"?â Kusalananda
3 hours ago
Related: grep with logic operators
â steeldriver
1 hour ago