Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make "grep" read patterns from a file?

Suppose there is a large text file and I would like to print only the lines that do not match some patterns. Obviously, I can use egrep -v 'patter1|pattern2|pattern3. Now what if all those patterns are in a text file ? What is the best way to make egrep read patterns from the file ?

like image 584
Michael Avatar asked Aug 11 '11 14:08

Michael


People also ask

Which option of grep takes patterns from file one line?

This can be used in grep to match the lines which end with the given string or pattern. 11. -f file option Takes patterns from file, one per line.

How do I search multiple patterns in one file?

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 search for a pattern in a file Linux?

Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file. The text search pattern is called a regular expression. When it finds a match, it prints the line with the result. The grep command is handy when searching through large log files.


2 Answers

grep -v -f pattern_file 
like image 165
Tomas Avatar answered Sep 21 '22 05:09

Tomas


egrep has an -f option which does exactly that: you specify a file, and it reads patterns from that file, one per line.

like image 34
Ernest Friedman-Hill Avatar answered Sep 21 '22 05:09

Ernest Friedman-Hill