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?
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?
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.
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.
┌──(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