NR==FNR trick for joining columns from two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NR==FNR trick for joining columns from two files
# 1  
Old 10-10-2011
NR==FNR trick for joining columns from two files

Code:
foo.txt
1       rs2887286       0       1145994 C       T
1       rs1240743       0       1323299 C       A
1       rs1695824       0       1355433 G       T
1       rs3766180       0       1468016 G       A
1       rs7519837       0       1500664 A       G
1       rs2272908       0       1711339 A       G

bar.txt
Code:
rs2887286       1145994 G       A
rs1240743       1323299 G       T
rs1695824       1355433 C       A
rs6603791       1490804 G       A
rs7519837       1500664 A       G
rs2272908       1711339 A       G

foobar.txt
Code:
1       rs2887286       0       1145994 C       T  1145994 G       A
1       rs1240743       0       1323299 C       A  1323299 G       T
1       rs1695824       0       1355433 G       T  1355433 C       A
1       rs6603791       0       1490804 G       A  
1       rs7519837       0       1500664 A       G  1500664 A       G
1       rs2272908       0       1711339 A       G  1711339 A       G

Would be nice to be able to do this with awk.
EDIT: Thank you for all the responses.
The other solutions seem to work only if the sort order of the foo.txt and bar.txt are equal. The files are NOT equal. One file has 400,000 lines (bar.txt) and the othr (foo.txt) has 11million lines.
My goal is to get the corresponding values for the rs### from foo.txt into a new output that has the bar.txt intact.
Thanks once again, my mistake for not explaining properly.

Last edited by genehunter; 10-11-2011 at 02:17 PM.. Reason: Changed the data to explain it better
# 2  
Old 10-10-2011
Code:
awk 'NR==FNR{a[$4]=$0;next}a[$2]{sub($1,x);$0=a[$1]$0}1' OFS=\\t foo.txt bar.txt


Last edited by danmero; 10-10-2011 at 10:27 PM..
# 3  
Old 10-11-2011
Hi try to use :
Code:
paste 1 2 | awk 'BEGIN { OFS="\t" } {print $1,$2,$3,$4,$5,$6,$8,$9,$10}'


Last edited by Franklin52; 10-11-2011 at 05:03 AM.. Reason: Please use code tags, thank you
# 4  
Old 10-11-2011
Quote:
Originally Posted by Mani_apr08
Hi try to use :

paste 1 2 | awk 'BEGIN { OFS="\t" } {print $1,$2,$3,$4,$5,$6,$8,$9,$10}'
hi you can try your command like thisSmilie
Code:
paste file1 file2 |awk '{for(i=1;i<NF;i++)if(i!=7){printf("%s\t",$i);}printf("%s\n",$NF);}'

# 5  
Old 10-11-2011
not a tab-delimited ..
Code:
$ paste file1 file2 | nawk '{$7= ""; print}'

# 6  
Old 10-11-2011
Code:
nawk '{$1=z}1' bar.txt | paste foo.txt -

# 7  
Old 10-11-2011
Danmero, Your solution works on the example data, but on real data it does not work. I am not sure what is the issue. The example is a few lines from the real data, so should have worked fine. Can you redo your solution to find the rs### column as the index to find the correspondng rs# from the other file and write the corresponding column? or give a brief description of what your solution means.. and I can play with it ?
Edit: Wish I could explain the above better..! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Joining Two Files Matching Two Columns

Hi All, I am looking to join two files where column 1 of file A matches with column 1 of file B and column 5 of files A matches with column 2 of file B. After joining the files based on above condition, out should contain entire line of file A and column 3, 4 and 5 of file B. Here is sample... (8 Replies)
Discussion started by: angshuman
8 Replies

2. Shell Programming and Scripting

Joining files using awk not extracting all columns from File 2

Hello All I'm joining two files using Awk by Left outer join on the file 1 File 1 1 AA 2 BB 3 CC 4 DD File 2 1 IND 100 200 300 2 AUS 400 500 600 5 USA 700 800 900 (18 Replies)
Discussion started by: venkat_reddy
18 Replies

3. UNIX for Dummies Questions & Answers

Joining different columns from multiple files

Hello again, I am trying to join 3rd column of 3 files into the end on one file and save it separately... my data looks like this file 1 Bob, Green, 80 Mark, Brown, 70 Tina, Smith, 60 file 2 Bob, Green, 70 Mark, Brown, 60 Tina, Smith, 50 file 3 Bob, Green, 50 Mark, Brown,60 Tina,... (6 Replies)
Discussion started by: A-V
6 Replies

4. Shell Programming and Scripting

Retain the unmatched columns while joining

I have 2 files file1 id city car type model 1 york subaru impreza king 2 kampala toyota corolla sissy 3 luzern chrysler gravity falcon file2 id name rating 3 zanzini PG 2 tara X when i use join sorted_file1 sorted_file2 >output i get something like... (2 Replies)
Discussion started by: anurupa777
2 Replies

5. Shell Programming and Scripting

Other alternative for joining together columns from multiple files

Hi again, I have monthly one-column files of roughly around 10 years. Is there a more efficient way to concatenate these files column-wise other than using paste command? For instance: file1.txt 12 13 15 12 file2.txt 14 15 18 19 file3.txt 20 21 (8 Replies)
Discussion started by: ida1215
8 Replies

6. Shell Programming and Scripting

Help with joining files and adding headers to files

Hi, I have about 20 tab delimited text files that have non sequential numbering such as: UCD2.summary.txt UCD45.summary.txt UCD56.summery.txt The first column of each file has the same number of lines and content. The next 2 column have data points: i.e UCD2.summary.txt: a 8.9 ... (8 Replies)
Discussion started by: rrdavis
8 Replies

7. Shell Programming and Scripting

Transposing column to row, joining with another file, then sorting columns

Hello! I am very new to Linux and I do not know where to begin... I have a column with >64,000 elements (that are not in numberical order) like this: name 2 5 9 . . . 64,000 I would like to transpose this column into a row that will later become the header of a very large file... (2 Replies)
Discussion started by: doobedoo
2 Replies

8. Shell Programming and Scripting

awk NR==FNR compare 2 files produce a 3rd

hi, i have two files, both with 3 columns, the 3rd column has common values between the two files and i want to produce a 3rd file with 4 columns. file 1 a, ,b c file 2 a, b ,d I want to compare the 3rd value and if a match print to file 3 with the 3 columns from the first file... (11 Replies)
Discussion started by: borderblaster
11 Replies

9. Shell Programming and Scripting

Joining two files based on columns/fields

I've got two files, File1 and File2 File 1 has got combination of col1, col2 and col3 which comes on file2 as well, file2 does not get col4. Now based on col1, col2 and col3, I would like to get col4 from file1 and all the columns from file2 in a new file Any ideas? File1 ------ Col1 col2... (11 Replies)
Discussion started by: rudoraj
11 Replies

10. Shell Programming and Scripting

Joining columns from two files, if the key matches

I am trying to join/paste columns from two files for the rows with matching first field. Any help will be appreciated. Files can not be sorted and may not have all rows in both files. Thanks. File1 aaa 111 bbb 222 ccc 333 File2 aaa sss mmmm ccc kkkk llll ddd xxx yyy Want to... (1 Reply)
Discussion started by: sk_sd
1 Replies
Login or Register to Ask a Question