15

I used comm to compare two sorted files. Each line in these files are positive integer numbers. But the results show

comm: file 1 is not in sorted order
comm: file 2 is not in sorted order

How come the error even if these two files are sorted?

2
  • 1
    In my case I had sorted (lexicographic ascending) the files using notepad++, which considers small case letters, and capital case letters separate eg. a will appear after 'Z' in ascending sort. This is different from how sort utility (bash) sorts. To verify this I converted all lines to upper case, and then sorted in np++, comm no longer complained. Sep 6, 2019 at 12:12
  • @SahilSingh no, the sort utility works like this as well.
    – bfontaine
    Aug 14, 2023 at 7:56

2 Answers 2

21

comm requires lexicographic sort (plain sort), not numeric sort (sort -n). For example, it wants the following order:

1
2000
300

Not the following order:

1
300
2000

Correct this and the problem should go away. For more esoteric cases where comm's locale may be different than the sort locale, you may want to run sort and comm with LC_COLLATE=C in their environment to use native byte ordering.

0
0

Pasting your sorted files in your question, or giving an insight on how you sorted the files would be helpful. In most cases the answer of @ChrisDown should work. My answer solves a problem that beginners might sometimes face.

sort file1.txt prints the sorted output, but does NOT change the original file. So when you use sort, it is important to check whether the files are actually sorted.

It will be clear in the output shown below.

┌──(user㉿user)-[~/Desktop/abc]
└─$ sort no1.txt                  
26
67
789
98
                                                                                
┌──(user㉿user)-[~/Desktop/abc]
└─$ cat no1.txt
789
67
98
26

Here sort gives us the sorted contents of the file, but when I see the contents of the file, it shows the original file itself. (Kindly refer to the end of this answer for the original txt files.)

One way in which you can pass on the sorted file to comm is shown below.

  • Sort each file and pipe it to cat to create a new file with the sorted contents.
  • Pass on the new files to comm.
┌──(user㉿user)-[~/Desktop/abc]
└─$ cat int1.txt
26
67
789
98
                                                                                
┌──(user㉿user)-[~/Desktop/abc]
└─$ sort no2.txt | cat > int2.txt
                                                                                
┌──(user㉿user)-[~/Desktop/abc]
└─$ comm int1.txt int2.txt
26
    45
        67
        789
    88
98

Text files:

  • no1.txt
    789
    67
    98
    26
    
  • no2.txt
    789
    67
    45
    88
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .