0

I used the sort command to numerically sort file1.txt.

~]# sort -n -o file1.txt file1.txt
0 Barack
50 George
60 Ronald
100 Bill

The sort check command states the file is not sorted.

~]# sort -c file1.txt
sort: file1.txt:4: disorder: 100 Bill

I manually modify the file and place "100 Bill" second.

~]# cat file1.txt
0 Barack
100 Bill
50 George
60 Ronald

Now, the sort check command produces no stdout or stderr, meaning the file is properly sorted. Once properly sorted, I can now join the files. It seems that the first character is being used to know if the file is sorted instead of the entire integer.

sort -c file1.txt

Is there a way to sort the file so that "100 Bill" is on line 2? I cannot do this manually, as the real application of this issue deals with hundreds of lines of text processed by a daily job.

  • 1
    The file is properly sorted in both cases. In the 1st case you sort it numerically, in the second case it's sorted lexicographically (via your edit - which is the same as running sort file1.txt). What is your actual question ? sort -c complains because you're asking it to verify if your file is sorted lexicographically: it isn't as you just sorted it numerically. Try with sort -c -n file1.txt. – don_crissti Jan 19 '18 at 13:40
  • I see. I falsely assumed that a numeric sort was required. I just tested with no options (lexicograpically) and the desired results are there. Thanks much. – JeremyCanfield Jan 19 '18 at 13:44
5

If you want to use a file with join, you need to make sure it’s sorted lexicographically on the join key, which means you shouldn’t use -n with sort. You can always sort the result of the join operation again numerically.

When you run sort -c, you need to specify the same sorting parameters as you used to sort the file: so if you want to check whether a file is sorted numerically, run sort -c -n.

Thus in your case, run sort -o file1.txt file1.txt to sort the file lexicographically for use with join (using the first field as the join field).

  • Thanks Stephen. This makes sense now. I mistakenly thought that numeric sort was required. I've made note of this. – JeremyCanfield Jan 19 '18 at 13:45

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.