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?
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With