Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[solved] grep "this but not followed by one of these"
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Gentoo Chat
View previous topic :: View next topic  
Author Message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3922
Location: Hamburg

PostPosted: Sat Apr 08, 2017 3:02 pm    Post subject: [solved] grep "this but not followed by one of these&qu Reply with quote

I'd like to ask for a single grep pattern which satisfies the condition to grep for all lines in a file containing string "foo" , if "foo" is not followed by "bar" or "bla".
So this should match : "prefix foo mampf postfix", but this not : "prefix foo bar postfix"


Last edited by toralf on Sat Apr 08, 2017 3:26 pm; edited 1 time in total
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Apr 08, 2017 3:25 pm    Post subject: Reply with quote

Build grep with the pcre USE flag as you need the "negative lookahead" feature it provides. Then your regex is straightforward:
Code:
grep -P 'foo (?!bar|bla)'
- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3922
Location: Hamburg

PostPosted: Sat Apr 08, 2017 3:26 pm    Post subject: Reply with quote

John R. Graham wrote:
Build grep with the pcre USE flag as you need the "negative lookahead" feature it provides. Then your regex is straightforward:
Code:
grep -P 'foo (?!bar|bla)'
- John
yep - thx
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sat Apr 08, 2017 3:26 pm    Post subject: Reply with quote

toralf ...

Code:
% cat input
prefix foo mampf postfix
prefix foo bar postfix
% grep -P 'foo.(?!bar)' input
prefix foo mampf postfix

edit ... JRG beat me to it ... damn ;)

HTH & best ... khay
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Apr 08, 2017 3:39 pm    Post subject: Reply with quote

:P

Two problems with my regex:
  1. It doesn't match foo at the end of a line.
  2. It doesn't take into account the possibility of non-space whitespace.
Solutions left as an exercise. ;)

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3922
Location: Hamburg

PostPosted: Sat Apr 08, 2017 3:55 pm    Post subject: Reply with quote

Hhm, so this is not the solution till now :
Code:
ssh2: (?!ED25519 SHA256:H7Rl8OShjUdLmaItsuIBt6sg44mhm6WLpXDoAAGVYck|ECDSA SHA256:vBwUgxXc/004y1INvwPxFtAP5CgHpJ1C2BEg2KRVnUo)
at least
Code:
grep -P -f watch-auth.txt /var/log/auth.log
doesn't spew out all other successful attempts.
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Apr 08, 2017 5:27 pm    Post subject: Reply with quote

You have an unescaped slash in that string. Take care with your escaping of characters that might have meaning in a regex context.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Gentoo Chat All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum