Join Question

Have two simple sorted text files:

file1
dog bark
cat meow
bird fly
goat baa

file2
dog loyal
cat herd
bird scratch
bird1 peck
goat milk

Using a simple join command is not picking up with word bird.

join file1 file2

Getting an error indicating that file2 is not sorted although it is. And bird is not being picked up.

This is the output after the error:

join: File 2 is not in sorted order
cat meow herd
dog bark loyal
goat baa milk

Any help would be appreciated.

Your sample file2 as displayed is not sorted on the first column.

$ join file1 file2
bird fly scratch
cat meow herd
dog bark loyal
goat baa milk

Owner@Owner-PC ~
$ cat file1
bird fly
cat meow
dog bark
goat baa

Owner@Owner-PC ~
$ cat file2
bird scratch
bird1 peck
cat herd
dog loyal
goat milk

Works when both are sorted

It's not working. The sort is correct, just not in my example.

The first sample file isn't sorted either. If the sample does not represent your real situation, then please post a representative sample.

Apologies, here is the actual sample:

>cat file1_sort
bird fly
cat meow
dog bark
goat baa

>cat file2_sort
bird1 peck
bird scratch
cat herd
dog loyal
goat milk

>join file1_sort file2_sort
join: File 2 is not in sorted order
cat meow herd
dog bark loyal
goat baa milk
$ sort -b file2_sort
bird scratch
bird1 peck
cat herd
dog loyal
goat milk

Scrutinizer, I'm doing the sort -b but it's returning the 'bird1' first. I think this is the problem. Is there a way to tell sort to put the numeric ones 2nd?

>sort -b file2_sort
bird1 peck
bird scratch
cat herd
dog loyal
goat milk

What happens when you use:

LC_ALL=C sort -b file2_sort

--
If no difference could you post

od -bc file2_sort

?

Hello jimmyf,

Yes, even I am getting the same result as you got(tested it in BASH) bird1 first, so could you please try following and let me know if this helps you.

sort -k1,1  Input_file

Output will be as follows.

bird scratch
bird1 peck
cat herd
dog loyal
goat milk

EDIT: Hello Scrutinizer,

Code LC_ALL=C sort -b has worked for me successfully.

Thanks,
R. Singh

1 Like

From man/info sort:

       *** WARNING *** The locale specified by the  environment  affects  sort
       order.  Set LC_ALL=C to get the traditional sort order that uses native
       byte values.
2 Likes

Yes that worked! Thanks Scrutinizer!