I have a file test-matching.txt
that looks like this:
ba
bababa
baba
babadooba
According to the grep man page, I should be able to get all but the first line using the expression
grep "ba{2,}" test-matching.txt
This should match all the lines containing instances of a string with 2 or more "ba's". However, when I run it, I get no output.
First I tried grep "ba" test-matching.txt
just to make sure it was working at all, and it gave me all four lines as output.
I've also tried the following, each with no output:
grep -e "ba{2,}" test-matching.txt
grep -e 'ba{2,}' test-matching.txt
grep -e "ba\\{2,\\}" test-matching.txt
grep 'ba{2,}' test-matching.txt
grep "ba\\{2,\\}" test-matching.txt
grep -e 'ba{2}' test-matching.txt
grep -e 'ba{2} test-matching.txt
What is the correct way match all the lines of "ba" concatenated 2 or more times?
Use egrep
or grep -E
(not grep -e
) if you want to use Extended regular expression syntax. If you want to use basic regular expression syntax, you need to backslash-escape the braces. Finally, if you want to repeat ba
, you need to group: egrep '(ba){2,}'
, or grep '\\(ba\\)\\{2,\\}'
if you prefer using basic regular expressions.
ba{2,}
hits only the a
baa
baaa
baaaa
etc
You need (ba){2,}
to make it works on group.
Try:
egrep "(ba){2,}" file
or
grep "\(ba\)\{2,\}" file
bababa
baba
babadooba
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.