Grep Exclude Multiple Strings

Exclude multiple patterns with ^

You can match match and then repeat at least 230 times (Or 250+ times) matching any of the listed characters [.,?!] not directly followed by

‘(?:(?![.,?!]’)[^’]){230,}

The pattern matches

  • Match the opening
  • (?: Non capture group
    • (?![.,?!]’) Negative lookahead, assert not one of . , ? or !
    • [^’] Match any char except the closing `’
  • ){230,} Repeat 230+ times

Regex demo

If there has to be a closing quote at the end, you can assert that using a positive lookahead (?=’)

‘(?:(?![.,?!]’)[^’]){230,}(?=’)

Regex demo

how to exclude multiple pattern using grep

Try below:

 grep -rI "PatternToSearch" ./path --exclude={*log*,tags}

Just use "," to separate patterns.

Seems duplicated with how do I use the grep --include option for multiple file types?

Grep exclude line only if multiple patterns match

These double checks in the same line are better done with awk:

awk '! (/"path": "\/"/ && /"User-Agent": "curl/)' file

This uses the logic awk '! (condition1 && condition2)', so it will just fail whenever both strings are found.

Test

$ cat a
"path": "/" and "User-Agent": "curl
"path": "/" hello
bye "User-Agent": "curl
this is a test
$ awk '! (/"path": "\/"/ && /"User-Agent": "curl/)' a
"path": "/" hello
bye "User-Agent": "curl
this is a test

Multiple grep piping (include+exclude) results in not showing anything

Your problem is one of buffering. grep is a tricky tool when it comes to that. From the man page:

By default, output is line buffered when standard output is a terminal and block buffered otherwise.

In your example, the first grep is buffering at the block level, so it will not turn an output to the 2nd grep for a while. The solution is to use the --line-buffered option to look like:

tail -F file.log | grep --line-buffered -ie 'error\|fatal\|exception\|shutdown\|started' | grep -vF '<DATATAG>'

How can I exclude one word with grep?

You can do it using -v (for --invert-match) option of grep as:

grep -v "unwanted_word" file | grep XXXXXXXX

grep -v "unwanted_word" file will filter the lines that have the unwanted_word and grep XXXXXXXX will list only lines with pattern XXXXXXXX.

EDIT:

From your comment it looks like you want to list all lines without the unwanted_word. In that case all you need is:

grep -v 'unwanted_word' file

Use grep --exclude/--include syntax to not grep through certain files

Use the shell globbing syntax:

grep pattern -r --include=\*.cpp --include=\*.h rootdir

The syntax for --exclude is identical.

Note that the star is escaped with a backslash to prevent it from being expanded by the shell (quoting it, such as --include="*.cpp", would work just as well). Otherwise, if you had any files in the current working directory that matched the pattern, the command line would expand to something like grep pattern -r --include=foo.cpp --include=bar.cpp rootdir, which would only search files named foo.cpp and bar.cpp, which is quite likely not what you wanted.

Update 2021-03-04

I've edited the original answer to remove the use of brace expansion, which is a feature provided by several shells such as Bash and zsh to simplify patterns like this; but note that brace expansion is not POSIX shell-compliant.

The original example was:

grep pattern -r --include=\*.{cpp,h} rootdir

to search through all .cpp and .h files rooted in the directory rootdir.



Related Topics



Leave a reply



Submit