Unix Join

Hi,

I am trying to join two simple files but unable to do so properly.
File R1 :
100
101
102
105
.
.
1000
10001

File R2
100|x1
102|x2
1000|a1
10001|a2

and when i do

 
    join -t \| -1 1 -2 1 R1 R2

i only get
100|x1
102|x2

why is 1000 and 100001 not coming?

Hi,

Join needs the default sort order, not a numerical one.. Try this if you are using bash / ksh93:

join -t \| <(sort -t\| -k1,1 R1) <(sort -t\| -k1,1 R2)
1 Like

Do you need the -a flag for join?

Hi Scrutinizer .. Thank you for helping me quickly. This is working .
I misunderstood that any sort will do as long as both files are having the same sort type.

The file R2 is actually very huge and when attempting to sort, I get this error

Is it better to join these files using awk than join..?

---------- Post updated at 04:05 PM ---------- Previous update was at 04:03 PM ----------

Hi elixir.. Yes, i need all records from first file.. and only the matching ones from second file.. I gave -a1 and -e "0"

Your temporary filesystem appears to be too small. You can specify an alternative location with sort's -T option...

... or try something like this:

awk -F\| 'NR==FNR{A[$1]; next} $1 in A' R1 R2
1 Like