[Solved] Combining columns from different files


 
Thread Tools Search this Thread
Operating Systems Linux [Solved] Combining columns from different files
# 1  
Old 07-08-2013
[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 ;
-------
Quote:
address1 name
address2 name
address3 name
File 2 ;
-------
Quote:
tel1 address1
tel2 address1
tel3 address1
tel1 address2
tel1 address3
My goal is to achieve a list with 3 columns as follows ;
Quote:
address1 tel1 name
address1 tel2 name
address1 tel3 name
address2 tel1 name
address3 tel1 name

Not sure whether I explained it OK, but hope so Smilie

Last edited by Don Cragun; 07-08-2013 at 08:01 PM.. Reason: Update title.
# 2  
Old 07-08-2013
Wouldn't it be better to have
Code:
address1 name1 tel1 tel2 tel3
address2 name2 tel1 tel2
etc...

? And certainly this would be sth. for a DB.
# 3  
Old 07-08-2013
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 ?
# 4  
Old 07-08-2013
Code:
awk 'NR==FNR{A[$1]=$2;next}A[$2]{$0=$2 OFS $1 OFS A[$2]}1' file1 file2

# 5  
Old 07-08-2013
Hey Yoda,

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

Now to dissect it and figure out what does what Smilie
# 6  
Old 07-08-2013
Code:
{print $2,$1,A[$2]}

is simpler than
Code:
{$0=$2 OFS $1 OFS A[$2]}1

Code:
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.
# 7  
Old 07-08-2013
@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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining certain columns of multiple files into one file

Hello Unix gurus, I have a large number of files (say X) each containing two columns of data and the same number of rows. I would like to combine these files to create a unique merged file containing X columns corresponding to the second column of each file (with a bonus of having the first... (3 Replies)
Discussion started by: ksennin
3 Replies

2. Shell Programming and Scripting

Join two files combining multiple columns and produce mix and match output

I would like to join two files when two columns in each file matches with each other and then produce an output when taking multiple columns. Like I have file A 1234,ABCD,23,JOHN,NJ,USA 2345,ABCD,24,SAM,NY,USA 5678,GHIJ,24,TOM,NY,USA 5678,WXYZ,27,MAT,NJ,USA and file B ... (2 Replies)
Discussion started by: mady135
2 Replies

3. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

4. Shell Programming and Scripting

Combining rows into columns

hi experts, I have a flat file with below contents Database1 Table1 column1 Database1 Table1 column2 Database1 Table1 column3 Database1 Table1 column4 Database1 Table2 Column1 Database1 Table2 Column2 Database2 Table1 Column1 Database2 Table1 Column2 Database2 Table1 Column3... (9 Replies)
Discussion started by: Selva_2507
9 Replies

5. Shell Programming and Scripting

Combining columns from multiple files into one single output file

Hi, I have 3 files with one column value as shown File: a.txt ------------ Data_a1 Data_a2 File2: b.txt ------------ Data_b1 Data_b2 Data_b3 Data_b4 File3: c.txt ------------ Data_c1 Data_c2 Data_c3 Data_c4 Data_c5 (6 Replies)
Discussion started by: vfrg
6 Replies

6. Shell Programming and Scripting

Combining columns from multiple files to one file

I'm trying to combine colums from multiple file to a single file but having some issues, appreciate your help. The filenames are the same except for the extension, path1.m0 --------- a b c d e f g h i path1.m1 --------- m n o p q r s t u File names are path1.m The... (3 Replies)
Discussion started by: rkmca
3 Replies

7. UNIX for Dummies Questions & Answers

Combining two text files as columns?

I have one space delimited file with multiple columns and one tab delimited file with multiple columns (They have the same number of rows). I want to basically combine these two text files into a new text file by column. How would I go about doing that? (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Combining columns from different files

I have two files I need to combine. The problem I'm having is I need to only combine data from the second file in the empty spaces of the first. For example: file1 Data Field Data Field Data Field Data Field file2 a - Insert Data b - Insert Data c - Insert Data d - Insert Data... (10 Replies)
Discussion started by: handband2
10 Replies

9. Shell Programming and Scripting

combining columns from different files

Hi all, I would be very grateful for some advice on the following. I have several text files. The files are experiment results with columns of data separated by white space. The files begin with several lines of header which are all preceeded by a comment character '#'. Each file has a... (10 Replies)
Discussion started by: iomaire
10 Replies

10. Shell Programming and Scripting

Combining Two fixed width columns to a variable length file

Hi, I have two files. File1: File1 contains two fixed width columns ID of 15 characters length and Name is of 100 characters length. ID Name 1-43<<11 spaces>>Swapna<<94 spaces>> 1-234<<10 spaces>>Mani<<96 spaces>> 1-3456<<9 spaces>>Kapil<<95 spaces>> File2: ... (4 Replies)
Discussion started by: manneni prakash
4 Replies
Login or Register to Ask a Question