0

I have 2 different files-

File 1

2
4
6
8
10
12

File 2

2
3
5
6
10
12

I want to compare 2 files and get the output data which is in File 1 but not in File 2-

Output

4
8

I am using below command but not getting desired output-

comm -23 file1 file2

  • I think the problem is comm expects a lexicographic sort which is different from the numeric order of your inputs. Lexicographically, 1, 10, and 12 all need to sort before 2, so you need to resort your file1 and file2 to use comm. – jw013 Jun 13 '18 at 15:34
  • 1
    You'd be getting an error from comm. It's always important to share these in your question. – roaima Jun 13 '18 at 19:07
4

For comm to work properly, both files have to be sorted lexicographically, not numerically. You may sort your files before calling comm using

sort -o file1 file1
sort -o file2 file2 

Then:

$ comm -23 file1 file2
4
8

Or, you may sort the files at the same time as you call comm, if your shell supports process substitutions:

$ comm -23 <( sort file1 ) <( sort file2 )
4
8
2

You can use grep

grep -F -x -f 'File2' -v 'File1'

Pattern of fixed--strings (-F) in File2 (-f)

Use -x to match whole lines. Otherwise "0123" in File1 would be excluded from the output due to "12" in File2.

find similar line and inverse with (-v)

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.