How can I find all lines containing two specified words?
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
I need to check if two (specified) words 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 file that contain the two words “cat†and “elephant†together (i.e., on the same line;
not necessarily side-by-side):
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
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 environments
I tried grep and awk with no hope. The problem is there are words that have upper and lower case, so how can I match for both words regardless of their letter status!?
text-processing awk grep
New contributor
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
10
down vote
favorite
I need to check if two (specified) words 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 file that contain the two words “cat†and “elephant†together (i.e., on the same line;
not necessarily side-by-side):
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
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 environments
I tried grep and awk with no hope. The problem is there are words that have upper and lower case, so how can I match for both words regardless of their letter status!?
text-processing awk grep
New contributor
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
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
19 hours ago
2
Related: grep with logic operators
– steeldriver
17 hours ago
How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
– phuclv
3 hours ago
3
Possible duplicate of How to run grep with multiple AND patterns?
– phuclv
3 hours ago
or Match two words that are on the same line
– don_crissti
7 mins ago
add a comment |Â
up vote
10
down vote
favorite
up vote
10
down vote
favorite
I need to check if two (specified) words 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 file that contain the two words “cat†and “elephant†together (i.e., on the same line;
not necessarily side-by-side):
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
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 environments
I tried grep and awk with no hope. The problem is there are words that have upper and lower case, so how can I match for both words regardless of their letter status!?
text-processing awk grep
New contributor
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I need to check if two (specified) words 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 file that contain the two words “cat†and “elephant†together (i.e., on the same line;
not necessarily side-by-side):
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
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 environments
I tried grep and awk with no hope. The problem is there are words that have upper and lower case, so how can I match for both words regardless of their letter status!?
text-processing awk grep
text-processing awk grep
New contributor
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 18 mins ago


G-Man
12k92860
12k92860
New contributor
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 19 hours ago
gormet
565
565
New contributor
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
gormet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
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
19 hours ago
2
Related: grep with logic operators
– steeldriver
17 hours ago
How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
– phuclv
3 hours ago
3
Possible duplicate of How to run grep with multiple AND patterns?
– phuclv
3 hours ago
or Match two words that are on the same line
– don_crissti
7 mins ago
add a comment |Â
2
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
19 hours ago
2
Related: grep with logic operators
– steeldriver
17 hours ago
How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
– phuclv
3 hours ago
3
Possible duplicate of How to run grep with multiple AND patterns?
– phuclv
3 hours ago
or Match two words that are on the same line
– don_crissti
7 mins ago
2
2
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
19 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
19 hours ago
2
2
Related: grep with logic operators
– steeldriver
17 hours ago
Related: grep with logic operators
– steeldriver
17 hours ago
How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
– phuclv
3 hours ago
How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
– phuclv
3 hours ago
3
3
Possible duplicate of How to run grep with multiple AND patterns?
– phuclv
3 hours ago
Possible duplicate of How to run grep with multiple AND patterns?
– phuclv
3 hours ago
or Match two words that are on the same line
– don_crissti
7 mins ago
or Match two words that are on the same line
– don_crissti
7 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
12
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
@glenn jackman suggested that 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
15 hours 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
15 hours ago
1
@glenn: IGNORECASE is indeed GNU;tolower($0)~/cat/
(or similarly withtoupper
) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
– dave_thompson_085
12 hours ago
add a comment |Â
up vote
10
down vote
$ grep -Fiw cat <file | grep -Fiw elephant
Cat is smaller than elephant
Elephant is larger than cat
We first extract all lines from the file file
that contains the word cat
and then narrow down those lines to the ones that contains the word elephant
.
This is done using grep -F -i -w
where
-F
makesgrep
treat the pattern as a fixed string, not as a regular expression,-i
makesgrep
do case-insensitive matching, and-w
makesgrep
match complete words only.
The -w
option is an extension of the POSIX standard for grep
, but is implemented by most common grep
implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.
Note that I'm not matching the line
Cat and elephants live in different environment
This is due to the final s
in elephants
. I would also not match the line
elephantiasis is catastrophic
for the same reason.
Would you want to allow for a plural s
at the end of words, use
$ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep
. The expressions will match an optional s
at the end of the two words. Now we match cat
and cats
(case-insensitively), but would not match catnip
, catsup
, or scat
.
add a comment |Â
up vote
8
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
2
down vote
You can do it in non-GNU awk
by using the “poor man’s†trick to get case insensitivity:
awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file
where, just as
[aeiou]
matches any one of a
, e
, i
, o
or u
, [Ee]
matches either E
or e
— that is, a case-insensitive match for “eâ€Â.Note that this approach (like all the other answers posted here so far)
will match the line
There are many ways to catch an elephant.
because the word “catch†contains the string “catâ€Â.ÂÂ
If you want to avoid this, try
awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' filewhere you constrain each word
to be preceded by a non-word character (or the beginning of the line)
and followed by a non-word character (or the end of the line) —
W
matches a non-word character (i.e., a space (or tab)or other non-alphanumeric * character).
(I’m not sure whether this is POSIX-compliant.)
Note that this will now not match
Cat and elephants live in different environmentsbecause the word “elephants†is not the same as the word “elephantâ€Â.
__________________
* In this context, underscore (the “_†character) counts as a letter.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
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
@glenn jackman suggested that 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
15 hours 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
15 hours ago
1
@glenn: IGNORECASE is indeed GNU;tolower($0)~/cat/
(or similarly withtoupper
) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
– dave_thompson_085
12 hours ago
add a comment |Â
up vote
12
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
@glenn jackman suggested that 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
15 hours 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
15 hours ago
1
@glenn: IGNORECASE is indeed GNU;tolower($0)~/cat/
(or similarly withtoupper
) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
– dave_thompson_085
12 hours ago
add a comment |Â
up vote
12
down vote
accepted
up vote
12
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
@glenn jackman suggested that 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
@glenn jackman suggested that awk
statement can be run as follows:
awk '/cat/&&/elephant/' IGNORECASE=1 file
edited 15 hours ago
answered 19 hours ago
Goro
10k64892
10k64892
Theprint $0
block is optional since it is the default action.
– glenn jackman
15 hours 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
15 hours ago
1
@glenn: IGNORECASE is indeed GNU;tolower($0)~/cat/
(or similarly withtoupper
) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
– dave_thompson_085
12 hours ago
add a comment |Â
Theprint $0
block is optional since it is the default action.
– glenn jackman
15 hours 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
15 hours ago
1
@glenn: IGNORECASE is indeed GNU;tolower($0)~/cat/
(or similarly withtoupper
) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.
– dave_thompson_085
12 hours ago
The
print $0
block is optional since it is the default action.– glenn jackman
15 hours ago
The
print $0
block is optional since it is the default action.– glenn jackman
15 hours 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
15 hours 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
15 hours ago
1
1
@glenn: IGNORECASE is indeed GNU;
tolower($0)~/cat/
(or similarly with toupper
) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.– dave_thompson_085
12 hours ago
@glenn: IGNORECASE is indeed GNU;
tolower($0)~/cat/
(or similarly with toupper
) is standard, but may give undesired results in some (non-English) locales with accented letters and especially Turkish with its dotted and dotless i's.– dave_thompson_085
12 hours ago
add a comment |Â
up vote
10
down vote
$ grep -Fiw cat <file | grep -Fiw elephant
Cat is smaller than elephant
Elephant is larger than cat
We first extract all lines from the file file
that contains the word cat
and then narrow down those lines to the ones that contains the word elephant
.
This is done using grep -F -i -w
where
-F
makesgrep
treat the pattern as a fixed string, not as a regular expression,-i
makesgrep
do case-insensitive matching, and-w
makesgrep
match complete words only.
The -w
option is an extension of the POSIX standard for grep
, but is implemented by most common grep
implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.
Note that I'm not matching the line
Cat and elephants live in different environment
This is due to the final s
in elephants
. I would also not match the line
elephantiasis is catastrophic
for the same reason.
Would you want to allow for a plural s
at the end of words, use
$ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep
. The expressions will match an optional s
at the end of the two words. Now we match cat
and cats
(case-insensitively), but would not match catnip
, catsup
, or scat
.
add a comment |Â
up vote
10
down vote
$ grep -Fiw cat <file | grep -Fiw elephant
Cat is smaller than elephant
Elephant is larger than cat
We first extract all lines from the file file
that contains the word cat
and then narrow down those lines to the ones that contains the word elephant
.
This is done using grep -F -i -w
where
-F
makesgrep
treat the pattern as a fixed string, not as a regular expression,-i
makesgrep
do case-insensitive matching, and-w
makesgrep
match complete words only.
The -w
option is an extension of the POSIX standard for grep
, but is implemented by most common grep
implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.
Note that I'm not matching the line
Cat and elephants live in different environment
This is due to the final s
in elephants
. I would also not match the line
elephantiasis is catastrophic
for the same reason.
Would you want to allow for a plural s
at the end of words, use
$ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep
. The expressions will match an optional s
at the end of the two words. Now we match cat
and cats
(case-insensitively), but would not match catnip
, catsup
, or scat
.
add a comment |Â
up vote
10
down vote
up vote
10
down vote
$ grep -Fiw cat <file | grep -Fiw elephant
Cat is smaller than elephant
Elephant is larger than cat
We first extract all lines from the file file
that contains the word cat
and then narrow down those lines to the ones that contains the word elephant
.
This is done using grep -F -i -w
where
-F
makesgrep
treat the pattern as a fixed string, not as a regular expression,-i
makesgrep
do case-insensitive matching, and-w
makesgrep
match complete words only.
The -w
option is an extension of the POSIX standard for grep
, but is implemented by most common grep
implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.
Note that I'm not matching the line
Cat and elephants live in different environment
This is due to the final s
in elephants
. I would also not match the line
elephantiasis is catastrophic
for the same reason.
Would you want to allow for a plural s
at the end of words, use
$ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep
. The expressions will match an optional s
at the end of the two words. Now we match cat
and cats
(case-insensitively), but would not match catnip
, catsup
, or scat
.
$ grep -Fiw cat <file | grep -Fiw elephant
Cat is smaller than elephant
Elephant is larger than cat
We first extract all lines from the file file
that contains the word cat
and then narrow down those lines to the ones that contains the word elephant
.
This is done using grep -F -i -w
where
-F
makesgrep
treat the pattern as a fixed string, not as a regular expression,-i
makesgrep
do case-insensitive matching, and-w
makesgrep
match complete words only.
The -w
option is an extension of the POSIX standard for grep
, but is implemented by most common grep
implementations. It basically disallows matches of the given patten when the matching string is part of a longer word.
Note that I'm not matching the line
Cat and elephants live in different environment
This is due to the final s
in elephants
. I would also not match the line
elephantiasis is catastrophic
for the same reason.
Would you want to allow for a plural s
at the end of words, use
$ grep -Eiw 'cats?' <file | grep -Eiw 'elephants?'
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environment
Here, we use an (extended) regular expression instead of a fixed string in both invocations of grep
. The expressions will match an optional s
at the end of the two words. Now we match cat
and cats
(case-insensitively), but would not match catnip
, catsup
, or scat
.
edited 11 hours ago
answered 11 hours ago


Kusalananda
110k15214338
110k15214338
add a comment |Â
add a comment |Â
up vote
8
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
8
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
8
down vote
up vote
8
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 hours ago
glenn jackman
49.2k467106
49.2k467106
add a comment |Â
add a comment |Â
up vote
2
down vote
You can do it in non-GNU awk
by using the “poor man’s†trick to get case insensitivity:
awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file
where, just as
[aeiou]
matches any one of a
, e
, i
, o
or u
, [Ee]
matches either E
or e
— that is, a case-insensitive match for “eâ€Â.Note that this approach (like all the other answers posted here so far)
will match the line
There are many ways to catch an elephant.
because the word “catch†contains the string “catâ€Â.ÂÂ
If you want to avoid this, try
awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' filewhere you constrain each word
to be preceded by a non-word character (or the beginning of the line)
and followed by a non-word character (or the end of the line) —
W
matches a non-word character (i.e., a space (or tab)or other non-alphanumeric * character).
(I’m not sure whether this is POSIX-compliant.)
Note that this will now not match
Cat and elephants live in different environmentsbecause the word “elephants†is not the same as the word “elephantâ€Â.
__________________
* In this context, underscore (the “_†character) counts as a letter.
add a comment |Â
up vote
2
down vote
You can do it in non-GNU awk
by using the “poor man’s†trick to get case insensitivity:
awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file
where, just as
[aeiou]
matches any one of a
, e
, i
, o
or u
, [Ee]
matches either E
or e
— that is, a case-insensitive match for “eâ€Â.Note that this approach (like all the other answers posted here so far)
will match the line
There are many ways to catch an elephant.
because the word “catch†contains the string “catâ€Â.ÂÂ
If you want to avoid this, try
awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' filewhere you constrain each word
to be preceded by a non-word character (or the beginning of the line)
and followed by a non-word character (or the end of the line) —
W
matches a non-word character (i.e., a space (or tab)or other non-alphanumeric * character).
(I’m not sure whether this is POSIX-compliant.)
Note that this will now not match
Cat and elephants live in different environmentsbecause the word “elephants†is not the same as the word “elephantâ€Â.
__________________
* In this context, underscore (the “_†character) counts as a letter.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can do it in non-GNU awk
by using the “poor man’s†trick to get case insensitivity:
awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file
where, just as
[aeiou]
matches any one of a
, e
, i
, o
or u
, [Ee]
matches either E
or e
— that is, a case-insensitive match for “eâ€Â.Note that this approach (like all the other answers posted here so far)
will match the line
There are many ways to catch an elephant.
because the word “catch†contains the string “catâ€Â.ÂÂ
If you want to avoid this, try
awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' filewhere you constrain each word
to be preceded by a non-word character (or the beginning of the line)
and followed by a non-word character (or the end of the line) —
W
matches a non-word character (i.e., a space (or tab)or other non-alphanumeric * character).
(I’m not sure whether this is POSIX-compliant.)
Note that this will now not match
Cat and elephants live in different environmentsbecause the word “elephants†is not the same as the word “elephantâ€Â.
__________________
* In this context, underscore (the “_†character) counts as a letter.
You can do it in non-GNU awk
by using the “poor man’s†trick to get case insensitivity:
awk '/[Cc][Aa][Tt]/ && /[Ee][Ll][Ee][Pp][Hh][Aa][Nn][Tt]/' file
where, just as
[aeiou]
matches any one of a
, e
, i
, o
or u
, [Ee]
matches either E
or e
— that is, a case-insensitive match for “eâ€Â.Note that this approach (like all the other answers posted here so far)
will match the line
There are many ways to catch an elephant.
because the word “catch†contains the string “catâ€Â.ÂÂ
If you want to avoid this, try
awk '/(^|W)[Cc][Aa]Tt/ && /(^|W)[Ee][Ll][Ee][Pp][Hh][Aa][Nn]Tt/' filewhere you constrain each word
to be preceded by a non-word character (or the beginning of the line)
and followed by a non-word character (or the end of the line) —
W
matches a non-word character (i.e., a space (or tab)or other non-alphanumeric * character).
(I’m not sure whether this is POSIX-compliant.)
Note that this will now not match
Cat and elephants live in different environmentsbecause the word “elephants†is not the same as the word “elephantâ€Â.
__________________
* In this context, underscore (the “_†character) counts as a letter.
answered 12 hours ago


G-Man
12k92860
12k92860
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-specified-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
2
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
19 hours ago
2
Related: grep with logic operators
– steeldriver
17 hours ago
How to run grep with multiple AND patterns?, How to use grep to match multiple strings in the same line?, grep for 2 words existing on the same line, Grep searching two words in a line
– phuclv
3 hours ago
3
Possible duplicate of How to run grep with multiple AND patterns?
– phuclv
3 hours ago
or Match two words that are on the same line
– don_crissti
7 mins ago