简体   繁体   中英

Finding a repeated string with grep and “bounds”

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:

  • With the -e option: grep -e "ba{2,}" test-matching.txt
  • With the -e option and single quotes: grep -e 'ba{2,}' test-matching.txt
  • With the -e option and escaped braces: grep -e "ba\\{2,\\}" test-matching.txt
  • Without the -e option and single quotes: grep 'ba{2,}' test-matching.txt
  • Without the -e option and escaped braces: grep "ba\\{2,\\}" test-matching.txt
  • With {2} instead of {2,}: grep -e 'ba{2}' test-matching.txt
  • With {2} instead of {2,} and the -e option: grep -e 'ba{2} test-matching.txt
  • etc.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM