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
What's the Reason Docker Ubuntu Official Image Would Exit Immediately When You Run/Start It
How to Redirect the Output of an Application in Background to /Dev/Null
How to Create a Directory and Give Permission in Single Command
How to Show the Wget Progress Bar Only
Sanitize Environment with Command or Bash Script
Is It Better to Use Git Grep Than Plain Grep If We Want to Search in Versioned Source Code
View Multi-Core or Mlti-Cpu Utlization on Linux
How to Run Processes Piped with Bash on Multiple Cores
Detect the Presence of a Device When It's Hot Plugged in Linux
How to Understand "Cmpl $0X0, -0X30(%Rbp)"/"Je ..."
Add Up a Column of Numbers at the Unix Shell
Move Window Between Tmux Clients
Linux: Which Process Is Causing "Device Busy" When Doing Umount
Importing a Cmake Project into Eclipse Cdt
How to Skip Saturday and Sunday in a Cron Expression
How to Fix Symbol Lookup Error: Undefined Symbol Errors in a Cluster Environment