Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not operator in regex [duplicate]

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?

like image 342
Swarnim Avatar asked Nov 22 '25 23:11

Swarnim


2 Answers

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
like image 66
anubhava Avatar answered Nov 25 '25 16:11

anubhava


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.

like image 22
Bohemian Avatar answered Nov 25 '25 16:11

Bohemian