2

I am joining two very simple sorted files but for some strange reason it is not working. f1.txt:

f1 abc
f2 mno
f3 pqr

f2.txt:

abc a1
mno a2
pqr a3

Command:

join -t '\t' f1.txt f2.txt -1 2 -2 1 > f3.txt

FYI in the f1,f2 files the space is a tab. But this is producing a blank f3.txt. Why is this happening? This is such a simple example of joining right?

1 Answers1

1

Your \t isn't being interpreted as a tab character. To do that you could/should use an ANSI string so your command would become

join -t $'\t' f1.txt f2.txt -1 2 -2 1 > f3.txt

with the $ before the '\t' so it will be interpreted as a tab like you want.

A handy resource for quoting things with bash at least is available here

Eric Renouf
  • 1,704
  • 1
  • 12
  • 20
  • Hi thanks. it worked. I have just one more question if you know. whats the difference between sort -k2 and sort -k2,2 . I saw some examples but didnt understand quite well. – Shabhri Naresh Sep 15 '16 at 16:51
  • @ShabhriNaresh in general new questions should be asked as a new question, but for now if you do `-k2` it will do sorting from field 2 until the end of the line as part of the sort, `-k2,2` will do the sort based only on field 2, and not consider any other fields. `-k2,3` would use just the second and third fields etc. The first number is the first field to consider, the second is the last – Eric Renouf Sep 15 '16 at 17:00