Grep

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    find out lines in a given file consisting of the following pattern BCAA, BCAAA, BCAAAA, BCAAAAA, BCAAAAAA

  2. Relevant commands, code, scripts, algorithms:

 grep BC(A)\{2,6} filename
  1. The attempts at a solution (include all code and scripts):
    bash: syntax error near unexpected token `('

  2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    BITS-pilani, Hyderabad,India, B.V. Prasad babu, System programming

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Try:

grep 'BC(A)\{2,6}' filename

Note the red single quotes

This time came up with new error

grep 'BC(A)\{2,6}' BCA.txt
Invalid \{\} repetition.

You don't need to escape {. It has special meaning in an extended regular expression. By escaping it, it loses its meaning. If you do, then escape the closing }.

Otherwise use egrep or grep -E (without the \ before {).

If 6 is the maximum number of "A"'s after "BC", then you need to define that in your regular expression ({2,6} doesn't mean there can't be a 7th, or more, consecutive "A").

I tried like this

egrep BC(A){2,6} BCA.txt
bash: syntax error near unexpected token `('

grep -E BC(A){2,6} BCA.txt
bash: syntax error near unexpected token `('

You need the quotes (note Jim's post).

Great it's working.. It came up with result

egrep 'BC(A){2,6}' BCA.txt
BCAAAAAAAAAAAAAA
BCAANBCAABCAABCAABCAA
BCBCBCCCBAAABCBCAAABCBCXA

grep -E 'BC(A){2,6}' BCA.txt
BCAAAAAAAAAAAAAA
BCAANBCAABCAABCAABCAA
BCBCBCCCBAAABCBCAAABCBCXA

Thanks a lot!!
:slight_smile:

That is correct. You need single quotes around
grep 'BC(A)\{2,6}' filename