Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two files A and B.

I want to find out which lines of file B are not present in the file A.

How do I do this? The lines can be at any position in file A

share|improve this question


grep -vxf A B

If you want the line number of the non-matching lines also:

grep -nvxf A B

If B has multiple non-matching identical lines, and you only want to print out the identical lines once, then

grep -vxf A B | sort | uniq  
share|improve this answer

you can sdiff command. Usage: sdiff file1.txt file2.txt

you can have the lines any where.

What are the uncommon lines are there they will be indicated with > symbol. Look at the below image. Example:

enter image description here

share|improve this answer

You say lines can be at any position, but if position is not important to you (just want to know the differences), use comm:

   Compare sorted files FILE1 and FILE2 line by line.

   With  no  options,  produce  three-column  output.  Column one contains
   lines unique to FILE1, column two contains lines unique to  FILE2,  and
   column three contains lines common to both files.

It requires files to be sorted, luckily sort can help with that. You can sort A > A.sorted to sort it, do something similar with B, and then:

comm -1 -3 A.sorted B.sorted

this will print "lines unique to FILE2" which in this case is B.sorted.

You can also do the sort inline, the command is a bit more cumbersome to type but you'd save a few steps:

comm -1 -3 <(sort A) <(sort B)
share|improve this answer

Your Answer


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.