The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merge Two Files based on First column apjneeraj Shell Programming and Scripting 10 04-13-2009 01:44 AM
Merge lines in Flat file based on first 5 characters Brado Shell Programming and Scripting 3 04-06-2009 02:57 PM
merge rows based on a common column smriti_shridhar Shell Programming and Scripting 6 10-17-2008 07:15 AM
compare the column from 3 files and merge that line ganesh_mak Shell Programming and Scripting 8 04-14-2008 08:56 AM
Merge files based on key sbasetty Shell Programming and Scripting 3 02-02-2007 06:05 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-20-2009
techmoris techmoris is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 27
merge two two txt files into one file based on one column

Hi,

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

Eg:

file1.txt

17328756,0000786623.pdf,0000786623
20115537,0000793892.pdf,0000793892

file2.txt

12521_74_4.zip,0000786623.pdf
12521_15_5.zip,0000793892.pdf

Desired Output


file3.txt

17328756,0000786623.pdf,0000786623,12521_74_4.zip
20115537,0000793892.pdf,0000793892,12521_15_5.zip

Please advice!!

Last edited by techmoris; 08-20-2009 at 02:12 PM..
  #2 (permalink)  
Old 08-20-2009
chompy chompy is offline
Registered User
  
 

Join Date: Aug 2009
Location: pwd
Posts: 65

Code:
#!/usr/bin/perl
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 (permalink)  
Old 08-20-2009
techmoris techmoris is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 27
I need this in unix

Thanks for your time and reply. I am looking in Unix.
  #4 (permalink)  
Old 08-20-2009
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,449
If you don't like perl try awk

Code:
awk -F, 'NR==FNR{_[$2]=$1;next}{$0=_[$2]?$0FS _[$2]:$0}1' file2 file1

  #5 (permalink)  
Old 08-20-2009
techmoris techmoris is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 27
great...

working fine..thx -
  #6 (permalink)  
Old 08-28-2009
chaos40 chaos40 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 2
Quote:
Originally Posted by danmero View Post
If you don't like perl try awk

Code:
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


thanks.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0