merge two two txt files into one file based on one column

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merge two two txt files into one file based on one column
# 1  
Old 08-20-2009
merge two two txt files into one file based on one column


I have file1.txt and file2.txt and would like to create file3.txt based on one column in UNIX





Desired Output



Please advice!!

Last edited by techmoris; 08-20-2009 at 02:12 PM..
# 2  
Old 08-20-2009
use Tie::File;
use strict;
use warnings;

my $output;
tie my @file1, "Tie::File", "file1.txt";
tie my @file2, "Tie::File", "file2.txt";

foreach my $line1 (@file1) {
        foreach my $line2 (@file2) {
        $output = join ",", $line1,$line2;
open (FILE, ">>", 'file3.txt') or die "Cannot open file!\n";
print FILE "$output\n";
close FILE;

# 3  
Old 08-20-2009
I need this in unix

Thanks for your time and reply. I am looking in Unix.
# 4  
Old 08-20-2009
If you don't like perl try awk Smilie
awk -F, 'NR==FNR{_[$2]=$1;next}{$0=_[$2]?$0FS _[$2]:$0}1' file2 file1

# 5  
Old 08-20-2009

working fine..thx Smilie-
# 6  
Old 08-28-2009
Originally Posted by danmero
If you don't like perl try awk Smilie
awk -F, 'NR==FNR{_[$2]=$1;next}{$0=_[$2]?$0FS _[$2]:$0}1' file2 file1

Sweet. What part of this command would I need to modify if I had 3 or more files that I want to output this way?

I actually have 5. file1 file2 file3 file4 file5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to merge two tables based on a matched column?

Hi, Please excuse me , i have searched unix forum, i am unable to find what i expect , my query is , i have 2 files of same structure and having 1 similar field/column , i need to merge 2 tables/files based on the one matched field/column (that is field 1), file 1:... (5 Replies)
Discussion started by: karthikram
5 Replies

2. Shell Programming and Scripting

To merge different sizes txt files

Hi, I have to .txt files that look like "baseMean" "log2FoldChange" "lfcSE" "stat" "pvalue" "padj" "c104215_g2_i4" 202.057864855455 5.74047973414006 1.14052672909697 5.03318299141063 4.8240223910525e-07 0.00234905721174879 "c91544_g1_i1" 373.123487095726 5.62496675850204 1.15060014539303... (2 Replies)
Discussion started by: alisrpp
2 Replies

3. UNIX for Dummies Questions & Answers

How to join 2 .txt files based on a common column?

Hi all, I'm trying to join two .txt file tab delimitated based on a common column. File 1 transcript_id gene_id length effective_length expected_count TPM FPKM IsoPct comp1000201_c0_seq1 comp1000201_c0 337 183.51 0.00 0.00 0.00 0.00 comp1000297_c0_seq1 ... (1 Reply)
Discussion started by: alisrpp
1 Replies

4. Shell Programming and Scripting

Merge files based on the column value

Hi Friends, I have a file file1.txt 1|ABC|3|jul|dhj 2|NHU|4|kil|eu 3|hjd|34|hfd|43 file2.txt 1||3|KING|dhj 2|NHU||k| 3|hjd|34|hd|43 i want to merge file1.txt file2.txt based on the column null values in file2.txif there are any nulls in column values , (5 Replies)
Discussion started by: i150371485
5 Replies

5. Shell Programming and Scripting

Merge CSV files and create a column with the filename from the original file

Hello everyone!! I am not completely new to shell script but I havent been able to find the answer to my problem and I'm sure there are some smart brains here up for the challenge :D. I have several CSV files that I need to combine into one, but I also need to know where each row came from.... (7 Replies)
Discussion started by: fransanchezoria
7 Replies

6. Shell Programming and Scripting

Help with merge two file based on similar column content

Input file 1: A1BG A1BG A1BG A1CF A1CF BCAS BCAS A2LD1 A2M A2M HAT . . Input file 2: A1BG All A1CF TEMP (5 Replies)
Discussion started by: perl_beginner
5 Replies

7. Shell Programming and Scripting

Merge two files based on a 3rd key file

Hi, I want to merge the two files based on the key file's columns. The key file: DATE~DATE HOUSE~IN_HOUSE CUST~IN_CUST PRODUCT~PRODUCT ADDRESS~CUST_ADDR BASIS_POINTS~BASIS_POINTS ... The other 2 files are From_file & To_file - The From_file: DATE|date/time|29|9 ... (9 Replies)
Discussion started by: dips_ag
9 Replies

8. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

9. Shell Programming and Scripting

Merge Two Files based on First column

Hi, I need to join two files based on first column of both files.If first column of first file matches with the first column of second file, then the lines should be merged together and go for next line to check. It is something like: File one: 110001 abc efd 110002 fgh dfg 110003 ... (10 Replies)
Discussion started by: apjneeraj
10 Replies

10. Shell Programming and Scripting

merge rows based on a common column

Hi guys, Please guide me if you have a solution to this problem. I have tried paste -s but it's not giving the desired output. I have a file with the following content- A123 box1 B345 bat2 C431 my_id A123 service C431 box1 A123 my_id I need two different outputs- OUTPUT1 A123... (6 Replies)
Discussion started by: smriti_shridhar
6 Replies
Login or Register to Ask a Question