2

I have a file and I wish to grep out all the lines that do not start with a timestamp. I tried using the following regex but it did not work:

cat myFile | grep '^(?!\[0-9\]$).*$'

Any other suggestions or something that I might be doing wrong here?

Toto
  • 89,455
  • 62
  • 89
  • 125
Swarnim
  • 93
  • 1
  • 2
  • 6

3 Answers3

9

Why not simply use grep -v option like this to negate:

grep -v "<pattern>" file

Let's say you want to grep all the lines in a shell script that are not commented ( do not have # at start ) then you can use:

grep -v "^\s*#" file.sh
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

Try this:

cat myFile | grep '^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d'

This assumes your timestamp is of the pattern dddd-dd-dd dd:dd:dd, but you change it to what matches your timestamp if it's something else.

Note: Unless you're using some kind of cmd chaining, grep pattern file is a simpler syntax

BTW: Your use of a double-negative makes me unsure if you want the timestamp lines or you want the non-timestamp lines.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You don't need a not operator, just use grep as it is most easily used: finding a pattern:

grep '^[0-9]' myFile
beerbajay
  • 19,652
  • 6
  • 58
  • 75