Issue
How can I use regex, with grep, to print all lines that do not contain a pattern. I cannot use -v
because I want to implement the regex within a much more complicated one, for which -v
would be impractical.
But whenever I try to print lines that do not contain a pattern, I do not get the expected output.
For example, if I have this file:
blah blah blah
hello world
hello people
something
And I want to print all lines that do not contain hello
, the output should be:
blah blah blah
something
I tried something like this, but it won't work:
egrep '[^hello]' file
All answers on SO use -v
, I can't find one using regex
Solution
You cannot use "whole" words inside of a character class. Your regular expression currently matches any character except: h
, e
, l
, o
. You could use grep with the following option and implement Negative Lookahead ...
grep -P '^(?!.*hello).*$' file
-P
option interprets the pattern as a Perl regular expression.
Ideone Demo
Answered By - hwnd Answer Checked By - Mildred Charles (WPSolving Admin)