Search for two sequential patterns with any number of characters in between using grep [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
Match two words that is on the same line
4 answers
I want to look for lines with "word1 ... word2" where '...' could be any different characters. So far I have used two greps for the same like this:
grep "$word1" $filename | grep "$word2"
Is there a faster way to do this by suppose something like this:
grep "$word1*$word2" $filename
where maybe * could be some special character which can be any other character(s)?
shell-script grep
marked as duplicate by Jeff Schaller, schily, slm⦠Jun 22 at 1:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
Match two words that is on the same line
4 answers
I want to look for lines with "word1 ... word2" where '...' could be any different characters. So far I have used two greps for the same like this:
grep "$word1" $filename | grep "$word2"
Is there a faster way to do this by suppose something like this:
grep "$word1*$word2" $filename
where maybe * could be some special character which can be any other character(s)?
shell-script grep
marked as duplicate by Jeff Schaller, schily, slm⦠Jun 22 at 1:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Match two words that is on the same line
4 answers
I want to look for lines with "word1 ... word2" where '...' could be any different characters. So far I have used two greps for the same like this:
grep "$word1" $filename | grep "$word2"
Is there a faster way to do this by suppose something like this:
grep "$word1*$word2" $filename
where maybe * could be some special character which can be any other character(s)?
shell-script grep
This question already has an answer here:
Match two words that is on the same line
4 answers
I want to look for lines with "word1 ... word2" where '...' could be any different characters. So far I have used two greps for the same like this:
grep "$word1" $filename | grep "$word2"
Is there a faster way to do this by suppose something like this:
grep "$word1*$word2" $filename
where maybe * could be some special character which can be any other character(s)?
This question already has an answer here:
Match two words that is on the same line
4 answers
shell-script grep
edited Jun 21 at 11:39
Jeff Schaller
30.8k846104
30.8k846104
asked Jun 21 at 11:39
Pratik Mayekar
1077
1077
marked as duplicate by Jeff Schaller, schily, slm⦠Jun 22 at 1:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jeff Schaller, schily, slm⦠Jun 22 at 1:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Use .*
:
grep "$word1.*$word2" "$filename"
.
matches any character*
matches any number of the preceding character
2
More precisely,.*
matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
â NickD
Jun 21 at 11:50
This is precisely what I was looking for
â Pratik Mayekar
Jun 21 at 11:52
add a comment |Â
up vote
0
down vote
If you need the two words to be delimited, i.e. if you do not want to match abba
if one of the words is bb
, then use
grep "\<$word1\>.*\<$word2\>" "$filename"
The pattern <
(here \<
to escape the first backslash from the shell) matches just before a word, and >
works similarly but just after a word.
There's alo b
that matches either before and after, and [[:<:]]
and [[:>:]]
that work just like <
and >
. Which ones that are implemented by grep
varies.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Use .*
:
grep "$word1.*$word2" "$filename"
.
matches any character*
matches any number of the preceding character
2
More precisely,.*
matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
â NickD
Jun 21 at 11:50
This is precisely what I was looking for
â Pratik Mayekar
Jun 21 at 11:52
add a comment |Â
up vote
2
down vote
accepted
Use .*
:
grep "$word1.*$word2" "$filename"
.
matches any character*
matches any number of the preceding character
2
More precisely,.*
matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
â NickD
Jun 21 at 11:50
This is precisely what I was looking for
â Pratik Mayekar
Jun 21 at 11:52
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Use .*
:
grep "$word1.*$word2" "$filename"
.
matches any character*
matches any number of the preceding character
Use .*
:
grep "$word1.*$word2" "$filename"
.
matches any character*
matches any number of the preceding character
edited Jun 21 at 11:53
answered Jun 21 at 11:43
RoVo
83219
83219
2
More precisely,.*
matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
â NickD
Jun 21 at 11:50
This is precisely what I was looking for
â Pratik Mayekar
Jun 21 at 11:52
add a comment |Â
2
More precisely,.*
matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
â NickD
Jun 21 at 11:50
This is precisely what I was looking for
â Pratik Mayekar
Jun 21 at 11:52
2
2
More precisely,
.*
matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).â NickD
Jun 21 at 11:50
More precisely,
.*
matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).â NickD
Jun 21 at 11:50
This is precisely what I was looking for
â Pratik Mayekar
Jun 21 at 11:52
This is precisely what I was looking for
â Pratik Mayekar
Jun 21 at 11:52
add a comment |Â
up vote
0
down vote
If you need the two words to be delimited, i.e. if you do not want to match abba
if one of the words is bb
, then use
grep "\<$word1\>.*\<$word2\>" "$filename"
The pattern <
(here \<
to escape the first backslash from the shell) matches just before a word, and >
works similarly but just after a word.
There's alo b
that matches either before and after, and [[:<:]]
and [[:>:]]
that work just like <
and >
. Which ones that are implemented by grep
varies.
add a comment |Â
up vote
0
down vote
If you need the two words to be delimited, i.e. if you do not want to match abba
if one of the words is bb
, then use
grep "\<$word1\>.*\<$word2\>" "$filename"
The pattern <
(here \<
to escape the first backslash from the shell) matches just before a word, and >
works similarly but just after a word.
There's alo b
that matches either before and after, and [[:<:]]
and [[:>:]]
that work just like <
and >
. Which ones that are implemented by grep
varies.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If you need the two words to be delimited, i.e. if you do not want to match abba
if one of the words is bb
, then use
grep "\<$word1\>.*\<$word2\>" "$filename"
The pattern <
(here \<
to escape the first backslash from the shell) matches just before a word, and >
works similarly but just after a word.
There's alo b
that matches either before and after, and [[:<:]]
and [[:>:]]
that work just like <
and >
. Which ones that are implemented by grep
varies.
If you need the two words to be delimited, i.e. if you do not want to match abba
if one of the words is bb
, then use
grep "\<$word1\>.*\<$word2\>" "$filename"
The pattern <
(here \<
to escape the first backslash from the shell) matches just before a word, and >
works similarly but just after a word.
There's alo b
that matches either before and after, and [[:<:]]
and [[:>:]]
that work just like <
and >
. Which ones that are implemented by grep
varies.
answered Jun 21 at 12:03
Kusalananda
101k13199312
101k13199312
add a comment |Â
add a comment |Â