Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Print lines of File 1 only if they don't contain patterns specified in File 2


$ cat File_1
L1  pattern1
L2  pattern     pattern2
L3  pattern3    pattern
L4  pattern4

$ cat File_2

Expected output

$cat File_3
L3  pattern3    pattern
L4  pattern4
share|improve this question
up vote 4 down vote accepted

You can use grep:

$ grep -vFf file2 file1
L3  pattern3    pattern
L4  pattern4

-v, -F and -f are defined by POSIX grep.

Note that the above will also match subpatterns. For example, if you have pattern in file2, that will match pattern1 in file1. To avoid that you can use -w (for GNU and BSD grep, maybe others):

$ grep -wvFf file2 file1
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.