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
  • 86,179
  • 61
  • 85
  • 118
Swarnim
  • 93
  • 2
  • 6

3 Answers3

8

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
  • 713,503
  • 59
  • 514
  • 593
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
  • 18,464
  • 6
  • 54
  • 74
0

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
  • 389,931
  • 88
  • 552
  • 692