Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

Exemple:

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

$ cat File_2
pattern1
pattern2

Expected output

$cat File_3
L3  pattern3    pattern
L4  pattern4
share|improve this question

1 Answer 1

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

 
discard

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.