Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a line does not start with a specific string with grep [duplicate]

Tags:

grep

shell

I have a file app.log

Oct 06 03:51:43 test test
Nov 06 15:04:53 text text text 
more text more text
Nov 06 15:06:43 text text text
Nov 06 15:07:33
more text more text
Nov 06 15:14:23  test test
more text more text
some more text 
Nothing but text
some extra text
Nov 06 15:34:31 test test test

How do I grep all the lines that does not begin with Nov 06 ?

I have tried

grep -En "^[^Nov 06]" app.log

I am not able to get lines which have 06 in them.

like image 543
gkmohit Avatar asked Nov 07 '14 15:11

gkmohit


1 Answers

Simply use the below grep command,

grep -v '^Nov 06' file

From grep --help,

-v, --invert-match        select non-matching lines

Another hack through regex,

grep -P '^(?!Nov 06)' file

Regex Explanation:

  • ^ Asserts that we are at the start.
  • (?!Nov 06) This negative lookahead asserts that there isn't a string Nov 06 following the line start. If yes, then match the boundary exists before first character in each line.

Another regex based solution through PCRE verb (*SKIP)(*F)

grep -P '^Nov 06(*SKIP)(*F)|^' file
like image 152
Avinash Raj Avatar answered Sep 22 '22 20:09

Avinash Raj