Merging Columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging Columns
# 1  
Old 02-26-2015
Merging Columns

Hi,

Can you please help me.

I have 2 files to merge

File1
Code:
1251 743
1250 742
1249 741
1248 749
1247 722
1246 740
1245 739
1244 740
1243 705
1242 631
1241 590

File2
Code:
1250 247
1249 241
1248 244
1247 300
1245 276
1244 292
1243 272
1242 236
1241 263

I have try
Code:
awk NR==FNR{a[$1]=$2;next}$1 in a{print $0,a[$1]} file1 file2

I get this
Code:
1250 247 742
1249 241 741
1248 244 749
1247 300 722
1245 276 739
1244 292 740
1243 272 705
1242 236 631
1241 263 590

But I will like something like this.
Code:
1251 743    
1250 742 247
1249 741 241
1248 749 244
1247 722 300
1246 740    
1245 739 276
1244 740 292
1243 705 272
1242 631 236
1241 590 263

How I can get my desired output..

Thanks Smilie
# 2  
Old 02-26-2015
Just flip the two files and remove the condition:
Code:
awk 'NR==FNR{a[$1]=$2;next} {print $0,a[$1]}' file2 file1

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-26-2015
Hello jiam912,

If order doesn't matter for you, following may help too.
Code:
awk 'FNR==NR{A[$1]=$2;next} ($1 in A){print $0 OFS A[$1];delete A[$1]} END{for(i in A){print i OFS A[i]}}'  file1 file2

Output will be as follows.
Code:
1250 247 742
1249 241 741
1248 244 749
1247 300 722
1245 276 739
1244 292 740
1243 272 705
1242 236 631
1241 263 590
1251 743
1246 740

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging and Adding colon to columns

Hello, I have a tab delim file that looks like this CHROM POS ID REF ALT ID HGVS_C HGVS_P 1 17319011 rs2076603 G A NM_022089.3,NM_001141973.2,NM_001141974.2 c.1815C>T,c.1800C>T,c.1800C>T p.Pro605Pro,p.Pro600Pro,p.Pro600Pro 1 20960230 rs45530340 ... (3 Replies)
Discussion started by: nans
3 Replies

2. Shell Programming and Scripting

Merging Multiple Columns between two files

Hello guys, I have 2 CSV files which goes like this: CSV1: Breaking.csv: UTF-8 "Name","Description","Occupation","Email" "Walter White","","Chemistry Teacher","w.w@bb.com" "Jessie Pinkman","","Junkie","j.p@bb.com" "Hank Schrader","","DEA Agent","h.s@bb.com" CSV2: Bad.csv... (7 Replies)
Discussion started by: jeffreybsu
7 Replies

3. Shell Programming and Scripting

Merging two columns into one

Suppose I have file1.txt 1 2 4 5 10 11 and I want to produce 1 2 4 5 10 11 file2.txt Thanks for your help :) (2 Replies)
Discussion started by: johnkim0806
2 Replies

4. UNIX for Dummies Questions & Answers

Merging two text files by two columns

Hi, I have two text files that I would like to merge/join. I would like to join them if the first columns of both text files match and the second column of the first text file matches the third column of the second text file. Example input: First file: 1334 10 0 0 1 5.2 1334 12 0 0 1 4.5... (4 Replies)
Discussion started by: evelibertine
4 Replies

5. Shell Programming and Scripting

Merging non-repeating columns of lines

Hello, I have file to work with. It has 5 columns. The first three, altogether, constitutes the position. The 4th column contains some values for downstream analysis and the fifth column contains some values that I want to add to 4th column (only if they happen to be in the same position). My... (5 Replies)
Discussion started by: menenuh
5 Replies

6. Shell Programming and Scripting

Merging columns from multiple files

Hello, I have a number of tab delimited data files consists of two columns. Like that: File1 800.000000 0.002744 799.000000 0.002517 798.000000 0.002836 797.000000 0.002553 FIle2 800.000000 0.000261 799.000000 0.000001 798.000000 0.000551 797.000000 0.000275 File3... (19 Replies)
Discussion started by: erden
19 Replies

7. UNIX for Dummies Questions & Answers

Merging two columns

Hi, I have two columns that look like this (tab seperated): name top carl ball bob lost joe smith I want the two columns to merge and look like this: nametop carlball boblost joesmith Also, I want to trim the edges of a column. So lets say the above column... (3 Replies)
Discussion started by: phil_heath
3 Replies

8. UNIX for Dummies Questions & Answers

Merging Non-Empty Columns within a CSV

I am trying to place all my data in a single row (order doesn't matter). Note I am a Unix novice, so please go easy on me. Here is an example Raw data: row# (1) 45 64 23 (2) 32 1 6 56 (3) 32 45 Needs to be like this: row# (1) 45 (2) 32 (3) 32 ... (2 Replies)
Discussion started by: mmann1123
2 Replies

9. Shell Programming and Scripting

Merging columns

Hi, I have input file. File1: Seqno Name 124 name1 121 name2 123 name3 122 name4 We will send the file1 to some other team. They will replace name column with place in file1 and send back to us as file2. file2: Seqno Place 124 place1 121 place2 123 place3file2: (5 Replies)
Discussion started by: manneni prakash
5 Replies

10. UNIX for Dummies Questions & Answers

Extracting columns from different files for later merging

Hello! I wan't to extract columns from two files and later combine them for plotting with gnuplot. If the files file1 and file2 look like: fiile1: a, 0.62,x b, 0.61,x file2: a, 0.43,x b, 0,49,x The desired output is a 0.62 0.62 b 0.61 0.49 Thank you in advance! (2 Replies)
Discussion started by: kingkong
2 Replies
Login or Register to Ask a Question