Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if all lines from one file are present somewhere in another file

Tags:

linux

grep

bash

awk

I used file1 as a source of data for file2 and now I need to make sure that every single line of text from file1 occurs somewhere in file2 (and find out which lines are missing, if any). It's probably important to note that while file1 has conveniently one search term per line, the terms can occur anywhere in the file2 including in the middle of a word. Also would help if the matching was case insensitive - doesn't matter if the text in file2 is even in all caps as long as it's there.

The lines in file1 include spaces and all sorts of other special characters like --.

like image 942
user2044638 Avatar asked Jan 02 '14 18:01

user2044638


1 Answers

if grep -Fqvf file2 file1; then
    echo $"There are lines in file1 that don’t occur in file2."
fi

Grep options mean:

-F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
-f, --file=FILE           obtain PATTERN from FILE
-v, --invert-match        select non-matching lines
-q, --quiet, --silent     suppress all normal output
like image 88
Dmitry Alexandrov Avatar answered Oct 07 '22 09:10

Dmitry Alexandrov