Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if all lines of a file are contained in another file

Tags:

file

linux

bash

I have a file: a.txt with a number at each line. I also have another file b.txt with also a number at each line.
How could I check if all the lines in file a.txt is included in b.txt?

like image 432
Jim Avatar asked Dec 10 '14 08:12

Jim


2 Answers

You can use comm for that.

If a.txt and b.txt are already sorted (lexically and ascending), you just need

comm -23 a.txt b.txt

or maybe

comm -23 a.txt b.txt | wc -l

If there is no output (or if wc -l returns "0"), then every line in a.txt was in b.txt (-2 suppresses output of lines that are only in b.txt, -3 suppresses output of lines that are in both files).

If the files are not sorted, you can use process substitution to pass a sorted output of each file to comm:

comm -23 <(sort a.txt) <(sort b.txt)

The process substitution <(COMMAND) puts the output of COMMAND into a FIFO or a file in /dev/fd (depending on what is supported on the system). On the commandline <(COMMAND) is then substituted with the name of this file as part of the command line expansion.

This does really check lines, so if a number exists twice in a.txt but only once in b.txt this will output the duplicate line from a.txt. If you do not care about duplicates, use sort -u FILE instead of sort FILE (or sort FILE | uniq in case your sort has no switch for unique sorting)

like image 94
Adaephon Avatar answered Sep 24 '22 06:09

Adaephon


You can use the diff command to compare two files

Example usage

$ seq 1 5 > a.txt
$ seq 1 5 > b.txt
$ diff a.txt b.txt
$
$ seq 1 6 > b.txt
$ diff a.txt b.txt
5a6
> 6

EDIT

You can also try something like

$ seq 1 5 > a.txt
$ seq 1 5 > b.txt
$ diff a.txt b.txt > /dev/null  && echo files are same || echo files are not same
files are same
$ seq 1 6 > b.txt
$ diff a.txt b.txt > /dev/null  && echo files are same || echo files are not same
files are not same
like image 21
nu11p01n73R Avatar answered Sep 24 '22 06:09

nu11p01n73R