Okay, there are several good ways to do this in the Linux CLI, each with its own strengths. Let's assume you have fileA.txt and fileB.txt.
Core Concepts:
-F (fixed string): Tells grep to treat the pattern as a literal string, not a regular expression. This is important if your lines might contain special regex characters.
-x (line regexp): Tells grep to match the entire line. This ensures "line X is in file Y" means an exact match of the whole line, not a partial match.
-f file: Tells grep to read patterns from file, one per line.
-v (invert match): Selects non-matching lines.
comm: Compares two sorted files line by line.
awk: A powerful text processing tool.
Here are the most common and effective methods:
Method 1: Using grep -f (Good for most cases, no sorting needed)
This is often the most straightforward and efficient method for unsorted files.
To find lines of File A that ARE contained in File B:
This command uses lines from fileB.txt as patterns to search within fileA.txt.
It will print the lines from File A that have an exact match in File B.
bash
grep -Fxf fileB.txt fileA.txt
grep: The command.
-F: Treat patterns as fixed strings.
-x: Match whole lines only.
-f fileB.txt: Read patterns (one per line) from fileB.txt.
fileA.txt: The file to search within.
To find lines of File A that ARE NOT contained in File B:
This uses the -v (invert match) option.
bash
grep -v -Fxf fileB.txt fileA.txt
-v: Invert the match; print lines from fileA.txt that do not have an exact match in fileB.txt.
Vice-versa:
To find lines of File B that ARE contained in File A:
bash
grep -Fxf fileA.txt fileB.txt
To find lines of File B that ARE NOT contained in File A:
bash
grep -v -Fxf fileA.txt fileB.txt
Method 2: Using comm (Requires files to be sorted)
comm is very efficient but requires both files to be sorted lexicographically first.
Or, using process substitution to avoid temporary files:
(This is generally preferred)
To find lines common to both File A and File B (lines of A in B, and lines of B in A):
comm outputs three columns:
Lines unique to file1
Lines unique to file2
Lines common to both
-12 suppresses columns 1 and 2, showing only common lines.
bash
comm -12 <(sort fileA.txt) <(sort fileB.txt)
Or with pre-sorted files:
bash
comm -12 fileA.sorted.txt fileB.sorted.txt
To find lines in File A that ARE NOT in File B:
-23 suppresses columns 2 and 3, showing only lines unique to file1 (A).
bash
comm -23 <(sort fileA.txt) <(sort fileB.txt)
Or with pre-sorted files:
bash
comm -23 fileA.sorted.txt fileB.sorted.txt
Vice-versa:
To find lines in File B that ARE NOT in File A:
-13 suppresses columns 1 and 3, showing only lines unique to file2 (B).
bash
comm -13 <(sort fileA.txt) <(sort fileB.txt)
Or with pre-sorted files:
bash
comm -13 fileA.sorted.txt fileB.sorted.txt
Method 3: Using awk (Flexible, no sorting needed)
awk can do this by loading one file into memory (as an associative array) and then processing the second file. This is efficient for moderately sized files where one can fit in memory.
To find lines of File A that ARE contained in File B:
Load fileB.txt into an array b_lines.
Then, for each line in fileA.txt, check if it exists as a key in b_lines.
bash
awk 'NR==FNR{b_lines[$0]; next} $0 in b_lines' fileB.txt fileA.txt
NR==FNR: This condition is true only while awk is reading the first file (fileB.txt).
b_lines[$0]: Adds the current line from fileB.txt as a key to the array b_lines. The value doesn't matter.
next: Skips to the next line of fileB.txt without further processing.
$0 in b_lines: This condition is checked for lines in the second file (fileA.txt). If the current line ($0) from fileA.txt exists as a key in b_lines, the default action (print the line) is performed.
To find lines of File A that ARE NOT contained in File B:
bash
awk 'NR==FNR{b_lines[$0]; next} !($0 in b_lines)' fileB.txt fileA.txt
The ! negates the condition, so it prints lines from fileA.txt not found in b_lines.
Vice-versa:
To find lines of File B that ARE contained in File A:
bash
awk 'NR==FNR{a_lines[$0]; next} $0 in a_lines' fileA.txt fileB.txt
To find lines of File B that ARE NOT contained in File A:
bash
awk 'NR==FNR{a_lines[$0]; next} !($0 in a_lines)' fileA.txt fileB.txt
Method 4: Using a loop with grep (Less efficient for large files)
This is conceptually simple but can be slow for large files because grep is invoked for every line of the first file.
To find lines of File A that ARE contained in File B:
bash
while IFS= read -r lineA; do
if grep -Fxq "$lineA" fileB.txt; then
echo "$lineA"
fi
done < fileA.txt
IFS= read -r lineA: Reads fileA.txt line by line safely.
grep -Fxq "$lineA" fileB.txt: Searches for the exact line (-Fx) quietly (-q). The if checks the exit status of grep (0 if found, non-zero otherwise).
To find lines of File A that ARE NOT contained in File B:
bash
while IFS= read -r lineA; do
if ! grep -Fxq "$lineA" fileB.txt; then # Note the "!"
echo "$lineA"
fi
done < fileA.txt
Vice-versa: Just swap fileA.txt and fileB.txt in the loop and variable names.
Choosing the right method:
grep -f: Generally a good default. It's often fast, doesn't require sorting, and is easy to understand. Best for "lines of A in B" or "lines of A NOT in B".
comm: Excellent and very fast if your files are already sorted or can be easily sorted. The process substitution comm -12 <(sort A) <(sort B) makes it convenient.
awk: Very powerful and flexible. Good when one file can comfortably fit into memory as an array. Often performs comparably to or better than grep -f for larger files.
Loop with grep: Use only for small files or when the logic is more complex than simple line presence, as it's the least performant for this specific task.
For most typical use cases, I'd recommend starting with grep -Fxf needles_file.txt haystack_file.txt or the awk approach. If performance is critical for huge files and they can be sorted, comm is king.