How to use grep to search word with specific number of occurence
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
How can I search a text in a file that matches such condition: starts with 'x' and and have at least 1 'a' immediately after the 'x' and at most 2 'a's immediately following the 'a'.
I tried something like
grep -i ^xa1..2 textfile.txt
but I think I am not understanding the usage of it
grep
add a comment |Â
up vote
0
down vote
favorite
How can I search a text in a file that matches such condition: starts with 'x' and and have at least 1 'a' immediately after the 'x' and at most 2 'a's immediately following the 'a'.
I tried something like
grep -i ^xa1..2 textfile.txt
but I think I am not understanding the usage of it
grep
so basically, you are looking for the pattern "xaaa" ?
â Alchemist
Oct 9 '17 at 3:18
You should add some examples of text input and an an example of the expected result. It looks like you don't want to matchxaaaax
. Please clearify by editing your question.
â hschou
Oct 9 '17 at 6:03
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How can I search a text in a file that matches such condition: starts with 'x' and and have at least 1 'a' immediately after the 'x' and at most 2 'a's immediately following the 'a'.
I tried something like
grep -i ^xa1..2 textfile.txt
but I think I am not understanding the usage of it
grep
How can I search a text in a file that matches such condition: starts with 'x' and and have at least 1 'a' immediately after the 'x' and at most 2 'a's immediately following the 'a'.
I tried something like
grep -i ^xa1..2 textfile.txt
but I think I am not understanding the usage of it
grep
grep
asked Oct 9 '17 at 3:12
user254679
1
1
so basically, you are looking for the pattern "xaaa" ?
â Alchemist
Oct 9 '17 at 3:18
You should add some examples of text input and an an example of the expected result. It looks like you don't want to matchxaaaax
. Please clearify by editing your question.
â hschou
Oct 9 '17 at 6:03
add a comment |Â
so basically, you are looking for the pattern "xaaa" ?
â Alchemist
Oct 9 '17 at 3:18
You should add some examples of text input and an an example of the expected result. It looks like you don't want to matchxaaaax
. Please clearify by editing your question.
â hschou
Oct 9 '17 at 6:03
so basically, you are looking for the pattern "xaaa" ?
â Alchemist
Oct 9 '17 at 3:18
so basically, you are looking for the pattern "xaaa" ?
â Alchemist
Oct 9 '17 at 3:18
You should add some examples of text input and an an example of the expected result. It looks like you don't want to match
xaaaax
. Please clearify by editing your question.â hschou
Oct 9 '17 at 6:03
You should add some examples of text input and an an example of the expected result. It looks like you don't want to match
xaaaax
. Please clearify by editing your question.â hschou
Oct 9 '17 at 6:03
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
$ cat ip.txt
xe
xa
xaaaalt
xaaa
xaa
$ # match 'a' 1 to 3 times
$ grep -i '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
$ # with ERE, no need to escape
$ grep -iE '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
To prevent matching more than three consecutive a
matching, you'd need
$ grep -iE '^xa1,3([^a]|$)' ip.txt
xa
xaaa
xaa
$ # or lookarounds with PCRE
$ grep -iP '^xa1,3(?!a)' ip.txt
xa
xaaa
xaa
add a comment |Â
up vote
0
down vote
You were close:
grep -i -E '^xaa0,2' textfile.txt
The syntax n,m
(with ,
and not ..
) is an extended regexp, hence the -E
option. and
need to be quoted so that the shell doesn't interpret them.
add a comment |Â
up vote
0
down vote
Given the input
1 x
2 xa
3 xaa
4 xaaa
5 xaaaa
Lines 2, 3 and 4 will be returned by
grep -wE 'xa1,3'
The extended regular expression xa1,3
will match any x
followed by between 1 and 3 a
. The -w
option to grep
makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
$ cat ip.txt
xe
xa
xaaaalt
xaaa
xaa
$ # match 'a' 1 to 3 times
$ grep -i '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
$ # with ERE, no need to escape
$ grep -iE '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
To prevent matching more than three consecutive a
matching, you'd need
$ grep -iE '^xa1,3([^a]|$)' ip.txt
xa
xaaa
xaa
$ # or lookarounds with PCRE
$ grep -iP '^xa1,3(?!a)' ip.txt
xa
xaaa
xaa
add a comment |Â
up vote
1
down vote
$ cat ip.txt
xe
xa
xaaaalt
xaaa
xaa
$ # match 'a' 1 to 3 times
$ grep -i '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
$ # with ERE, no need to escape
$ grep -iE '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
To prevent matching more than three consecutive a
matching, you'd need
$ grep -iE '^xa1,3([^a]|$)' ip.txt
xa
xaaa
xaa
$ # or lookarounds with PCRE
$ grep -iP '^xa1,3(?!a)' ip.txt
xa
xaaa
xaa
add a comment |Â
up vote
1
down vote
up vote
1
down vote
$ cat ip.txt
xe
xa
xaaaalt
xaaa
xaa
$ # match 'a' 1 to 3 times
$ grep -i '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
$ # with ERE, no need to escape
$ grep -iE '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
To prevent matching more than three consecutive a
matching, you'd need
$ grep -iE '^xa1,3([^a]|$)' ip.txt
xa
xaaa
xaa
$ # or lookarounds with PCRE
$ grep -iP '^xa1,3(?!a)' ip.txt
xa
xaaa
xaa
$ cat ip.txt
xe
xa
xaaaalt
xaaa
xaa
$ # match 'a' 1 to 3 times
$ grep -i '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
$ # with ERE, no need to escape
$ grep -iE '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa
To prevent matching more than three consecutive a
matching, you'd need
$ grep -iE '^xa1,3([^a]|$)' ip.txt
xa
xaaa
xaa
$ # or lookarounds with PCRE
$ grep -iP '^xa1,3(?!a)' ip.txt
xa
xaaa
xaa
edited Oct 9 '17 at 4:12
answered Oct 9 '17 at 4:06
Sundeep
6,9611826
6,9611826
add a comment |Â
add a comment |Â
up vote
0
down vote
You were close:
grep -i -E '^xaa0,2' textfile.txt
The syntax n,m
(with ,
and not ..
) is an extended regexp, hence the -E
option. and
need to be quoted so that the shell doesn't interpret them.
add a comment |Â
up vote
0
down vote
You were close:
grep -i -E '^xaa0,2' textfile.txt
The syntax n,m
(with ,
and not ..
) is an extended regexp, hence the -E
option. and
need to be quoted so that the shell doesn't interpret them.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You were close:
grep -i -E '^xaa0,2' textfile.txt
The syntax n,m
(with ,
and not ..
) is an extended regexp, hence the -E
option. and
need to be quoted so that the shell doesn't interpret them.
You were close:
grep -i -E '^xaa0,2' textfile.txt
The syntax n,m
(with ,
and not ..
) is an extended regexp, hence the -E
option. and
need to be quoted so that the shell doesn't interpret them.
answered Oct 9 '17 at 3:18
xhienne
11.7k2553
11.7k2553
add a comment |Â
add a comment |Â
up vote
0
down vote
Given the input
1 x
2 xa
3 xaa
4 xaaa
5 xaaaa
Lines 2, 3 and 4 will be returned by
grep -wE 'xa1,3'
The extended regular expression xa1,3
will match any x
followed by between 1 and 3 a
. The -w
option to grep
makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).
add a comment |Â
up vote
0
down vote
Given the input
1 x
2 xa
3 xaa
4 xaaa
5 xaaaa
Lines 2, 3 and 4 will be returned by
grep -wE 'xa1,3'
The extended regular expression xa1,3
will match any x
followed by between 1 and 3 a
. The -w
option to grep
makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Given the input
1 x
2 xa
3 xaa
4 xaaa
5 xaaaa
Lines 2, 3 and 4 will be returned by
grep -wE 'xa1,3'
The extended regular expression xa1,3
will match any x
followed by between 1 and 3 a
. The -w
option to grep
makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).
Given the input
1 x
2 xa
3 xaa
4 xaaa
5 xaaaa
Lines 2, 3 and 4 will be returned by
grep -wE 'xa1,3'
The extended regular expression xa1,3
will match any x
followed by between 1 and 3 a
. The -w
option to grep
makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).
answered Oct 9 '17 at 5:57
Kusalananda
105k14209326
105k14209326
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%2f396926%2fhow-to-use-grep-to-search-word-with-specific-number-of-occurence%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
so basically, you are looking for the pattern "xaaa" ?
â Alchemist
Oct 9 '17 at 3:18
You should add some examples of text input and an an example of the expected result. It looks like you don't want to match
xaaaax
. Please clearify by editing your question.â hschou
Oct 9 '17 at 6:03