5

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

1
5

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  
2

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

1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.