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
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
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
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
New contributor
edited 18 mins ago
G-Man
12k92860
12k92860
New contributor
asked 19 hours ago
gormet
565
565
New contributor
New contributor
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