Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep a file with a list of regular expressions

Tags:

regex

grep

I have a large file of regular expressions, one per line. I would like to inverse grep another multi-line file against any regular expression that appears in the first file. Something like this:

grep -v fileWithManyRegularExpressions fileThatMightMatchSomeRegularExpressions

Is there an elegant method to do this aside from looping through every regular expression?

like image 244
Rich Avatar asked Jul 22 '10 15:07

Rich


People also ask

Can you use regular expressions with grep?

You can also use the grep command to search for targets that are defined as patterns by using regular expressions. Regular expressions consist of letters and numbers, in addition to characters with special meaning to grep . These special characters, called metacharacters, also have special meaning to the system.

How do I grep with multiple patterns?

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path. The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.

How do you grep a regular expression in Linux?

Grep Regular Expression In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp ) option.

How do you grep pattern in a file?

To find a pattern that is more than one word long, enclose the string with single or double quotation marks. The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.


2 Answers

grep -v -f regexes.txt content.txt
like image 178
SilentGhost Avatar answered Sep 17 '22 12:09

SilentGhost


At least with Gnu grep you can use --file=<filename> and all should be well.

like image 34
Jerry Coffin Avatar answered Sep 20 '22 12:09

Jerry Coffin