5

I am grepping date 2019-09-XX and 2019-10-XX but somehow my grep not helping out, i am sure i am missing something here

  Last Password Change: 2019-10-30
  Last Password Change: 2017-02-07
  Last Password Change: 2019-10-29
  Last Password Change: 2019-11-03
  Last Password Change: 2019-10-31
  Last Password Change: 2018-09-27
  Last Password Change: 2018-09-27
  Last Password Change: 2019-06-27

I am doing following and it doesn't work

grep "2019\-[09,10]\-" file also tried grep "2019\-{09,10}\-" file

Satish
  • 1,652
  • 1
    [] matches one character of those it has inside, so your first regex would match 2019-,- for example. {} determines how many characters must be matched for the previous matching atom, in your case -, so your second regex would match 2019-----------. That's in extended regular expressions, though. Since you didn't supply -E or -P, then {} doesn't have special meaning. So, it matches literally 2019-{09,10}-. – JoL Nov 14 '19 at 01:41

3 Answers3

7

You want the "alternation" regular expression token | to say "either this or that":

grep -E '2019-(09|10)-' file

See Why does my regular expression work in X but not in Y? for some background on regular expression tokens and regex classes (basic, extended, etc).

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
2

grep isn't good at handling numbers, it doesn't know how to compare them arithmetically. For that, you may want to use something like awk or Perl. That's not very important here, since it's easy to just list out 09 and 10, but if you had something like a range from 97 to 123 it would be much worse.

E.g. this would pick they year, month and day as numbers, and print out the lines where the day is between 27 and 31:

perl -ne 'print if /Last Password Change: ([0-9]+)-([0-9]+)-([0-9]+)/ && $3 >= 27 && $3 <= 31' < file

The regex is mostly like a grep ERE, the parenthesis capture the corresponding parts into the variables $1, $2, $3 etc.

ilkkachu
  • 138,973
0

Just to add an alternative:

grep -e "2019-10-.." -e "2019-09-.." file