[Solved] Combining columns from different files

Hey Guys & Gals,

I am stuck with the following ;

I have 2 text files, each containing 2 columns.

My goal is to have a column from the 2nd file placed inbetween the columns in the first file.
Basically the idea is, each address has a different name (but 1 name per address) but 1 address can have more telephone numbers and I would like to list the tel. numbers per address in the order as mentioned below
(address - telephone - name)

File 1 ;
-------

File 2 ;
-------

My goal is to achieve a list with 3 columns as follows ;

Not sure whether I explained it OK, but hope so :wink:

Wouldn't it be better to have

address1 name1 tel1 tel2 tel3
address2 name2 tel1 tel2
etc...

? And certainly this would be sth. for a DB.

Hey RudiC,

Well, one way or the other, what I am looking for is some guidance in the correct way one could use the usual linux commands (grep / awk etc) to accomplish the listing as per my example.

Any ideas ?

awk 'NR==FNR{A[$1]=$2;next}A[$2]{$0=$2 OFS $1 OFS A[$2]}1' file1 file2

Hey Yoda,

That looks like it will do the trick ! Great, many thanks.

Now to dissect it and figure out what does what :wink:

{print $2,$1,A[$2]}

is simpler than

{$0=$2 OFS $1 OFS A[$2]}1
awk 'NR==FNR {A[$1]=$2; next} {print $2,$1,A[$2]}' file1 file2

If 1st file, store field 2 in A indexed by field 1.
Otherwise (file 2) print line augmented by value from A.

@MadeInGermany

Works great as well, thanks.

I am always blown away by how easy you guys make this all look..

Thanks for the replies, my query solved :smiley: