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

  • you must escape the {} as \{\} – tachomi Oct 6 '15 at 21:59
  • grep '^[Oo]cc' filename – cas Oct 6 '15 at 22:08
  • i tried doing that @cas it did not work – agent00uche Oct 6 '15 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 '15 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. – agent00uche Oct 6 '15 at 22:19
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
| improve this answer | |
  • -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 '15 at 23:03
  • @cas good point, answer edited. – terdon Oct 7 '15 at 11:46
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.

| improve this answer | |
  • @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 '15 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.