0

how do i write a regular expression using grep to find lines that start with 'O' or 'o' AND contain 2 consecutive 'c' characters.

grep '^[Oo]c{2}' filename

8
  • you must escape the {} as \{\}
    – tachomi
    Oct 6, 2015 at 21:59
  • grep '^[Oo]cc' filename
    – cas
    Oct 6, 2015 at 22:08
  • i tried doing that @cas it did not work Oct 6, 2015 at 22:09
  • You must have mistyped it then, because it certainly does work. there's no functional difference between ^[Oo]c\{2\} and ^[Oo]cc. e.g. try grep -c '^[Oo]cc' /usr/share/dict/words, that will count all words beginning with Occ or occ (on my system, 123 such words)
    – cas
    Oct 6, 2015 at 22:12
  • yes i apologize your example did work! but for some reason the given file name i am suppose to use is not displaying the output of the expression. Oct 6, 2015 at 22:19

2 Answers 2

5

You were 99% there, very close, just:

grep '^[Oo]c\{2\}' filename

or equivalent:

grep -E '^[Oo]c{2}' filename

Regular expressions

  • as you know ^[Oo]c , the caret ^ matches beginning, and any of the characters within [] are matched
  • you were very close with c{2}, where you meant to match the previous character c, exactly two times.
  • however grep by default interprets Basic Regular Expressions which does not interpret { as a meta-character for use with this kind of matching, so we change it to \{ and \} to make it recognize it
  • alternatively you could enable Extended Regular Expressions to avoid the backslash: grep -E '^[Oo]c{2}' filename

If that does not match, maybe copy a sample of the contents of your test filename that does include what you are trying to match, and update the question to display it, in case there is something else we missed.

1
  • @agent00uche If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.
    – terdon
    Oct 6, 2015 at 22:46
1

You already have a pure regex answer but here's a more grepish one:

grep -i ^occ file

The -i flag tells grep to do case insensitive matching. It is defined by POSIX so should be present in any grep implementation. Once you have that, since you only want two c characters, there's no reason to complicate things by using {2}; cc is both shorter and clearer. However, note that the -i applies to the entire reguex so this will also match cC or CC or Cc. If you don't want that, use @user454038's approach instead.

If you also need to match lines where the consecutive cs are not directly after the o, use this instead:

grep -i '^o.*cc' file
2
  • -i makes the entire regexp case-insensitive, i.e. it will match CC, Cc, and cC as well as cc. If the homework question does not explicitly say that's allowed, the OP will probably be marked down for it.
    – cas
    Oct 6, 2015 at 23:03
  • @cas good point, answer edited.
    – terdon
    Oct 7, 2015 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .