Exclude dash in grep

Hi,

I must be overlooking something, but I don't understand why this doesn't work. I'm trying to grep on a date, excluding all the lines starting with a dash:

testfile:

#2013-12-31
2013-12-31

code:

grep '^[^#]2013-12-31' testfile

I'm expecting to see just the second line '2013-12-31' but I don't get any results. grep -v is not an option btw.

[^#] means a single character that is not a hash-sign.
Why not use:

grep '^2013-12-31'

But perhaps you mean this:

grep '^[^#]*2013-12-31'
1 Like

Elementary. [^#] will match one character which is not an octothorpe (or hash). So, what you are asking grep to match is one non-hash character (mandatory for the overall pattern to match) at the beginning of a line followed by 2013-12-31 .

Get it?

1 Like

The problem is that the string doesn't always start with the date. It could be @2013-12-31 for example.

grep '^[^#]*2013-12-31'

seems to work by the way. Thanks :slight_smile: