Is it possible to exclude exact word(s) in (e)grep using a pattern only without using -v option. For example, since ()
groups a pattern or an exact string (), I thought egrep [^(main)]
will match substrings without the exact word main
but what happens is it excludes the letter m,a,i,n
. I'm still learning regular expression in bash.

- 91
- 10
-
Possible duplicate of [Regular expression to match a line that doesn't contain a word](https://stackoverflow.com/questions/406230/regular-expression-to-match-a-line-that-doesnt-contain-a-word) – vintnes May 01 '19 at 19:18
-
There's several things you might mean by that. [edit] your question to show concise, testable sample input and expected output so we can help you. – Ed Morton May 01 '19 at 21:31
3 Answers
You can use this negative look ahead regex for rejecting a line that contains main
as whole word and don't have to use -v
necessarily.
grep -P '^(?!.*\bmain\b).*$' bash.txt
Here, -P
enables Perl regular expression
Before are the contents of a file named bash.txt,
hello 123
hello world
hello main world
some main text
some mainly text
And upon running above command it prints this output,
hello 123
hello world
some mainly text

- 18,127
- 2
- 19
- 36
The reason
egrep [^(main)] excludes the letter m,a,i,n.
is that [^(main)] matches a single character that is not contained within the brackets, which excludes not only m,a,i,n, but also ( and ). You can find the correct usage of this feature here.
Actually, there is already a similar StackOverflow question. It has several forms of negative look-ahead. ^((?!main).)*$
may be one of them satisfying your need.

- 136
- 1
- 9
idk if it's what you want or not but borrowing @Pushpesh's example then with any POSIX awk:
$ awk '!/(^|[^[:alpha:]])main([^[:alpha:]]|$)/' file
hello 123
hello world
some mainly text
or using GNU tools for word boundaries:
$ awk '!/\<main\>/' file
hello 123
hello world
some mainly text
$ sed '/\<main\>/d' file
hello 123
hello world
some mainly text

- 188,023
- 17
- 78
- 185