Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

3 Answers 3

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

Simply

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 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

 
discard

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.