Join command

Hi,

I am trying to join 2 files..viz

f1:
12~a1
13~a2
112~a3
1112~a4

f2:
12~fa2
13~fa2
112~fa3
1112~fa4

while I join..I just get 2 o/p..
12~a1~fa2
13~a2~fa2

while the rest doesnt appear:-(

Can any1 help me in this plss?

Thanx
Sam

You can use -o option to specify the fields you want to see in the output.

join -o 1.1 1.2 2.1 2.2 file1 file2

will show the first two fields for both files in one line.

Try man join.

You can also try the paste command.

paste file1 file2

The join command requires that the files be sorted in lexical sequence. Your files are sorted by numerical sequence.

$ cat f1
12~a1
13~a2
112~a3
1112~a4
$ cat f2
12~fa2
13~fa2
112~fa3
1112~fa4
$ join -t '~' f1 f2
12~a1~fa2
13~a2~fa2
$ sort f1 > s1
$ sort f2 > s2
$ join -t '~' s1 s2
1112~a4~fa4
112~a3~fa3
12~a1~fa2
13~a2~fa2

I've just realised that you don't have to sort the files if you use awk...

$ awk 'BEGIN{FS=OFS="~"}FNR==NR{a[$1]=$2}FNR!=NR{print $1,a[$1],$2}' f1 f2
12~a1~fa2
13~a2~fa2
112~a3~fa3
1112~a4~fa4