Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Merging two files

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-28-2013
saj saj is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 8 March 2013, 7:24 AM EST
Posts: 29
Thanks: 4
Thanked 0 Times in 0 Posts
Merging two files

Hi All ,

I have a scenario where we need to combine two files .
Below are the sample files and expected output ,

File 1:

Code:
1|ab
1|ac
1|ae
2|ad
2|ac

File 2:

Code:
1|xy
1|fc
2|gh
2|ku

Output file :

Code:
1|ab|xy
1|ab|fc
1|ac|xy
1|ac|fc
1|ae|xy
1|ae|fc
2|ad|gh
2|ad|ku
2|ac|gh
2|ac|ku

Please help us how this can be done in unix .

Moderator's Comments:
Use code tags, see PM.

Last edited by saj; 02-28-2013 at 07:21 AM..
Sponsored Links
    #2  
Old Unix and Linux 02-28-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 1 September 2015, 1:18 PM EDT
Location: Chennai
Posts: 573
Thanks: 50
Thanked 75 Times in 73 Posts
this should help you


Code:
join -t"|" file1 file2

The Following 2 Users Say Thank You to PikK45 For This Useful Post:
panyam (02-28-2013), Scrutinizer (03-01-2013)
Sponsored Links
    #3  
Old Unix and Linux 02-28-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 9 July 2015, 10:56 AM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts

Code:
awk -F "|" 'NR==FNR{X[$1,++Y[$1]]=$2;next}
    {for(i=1;i<=Y[$1];i++){print $1,$2,X[$1,i]}}' OFS="|" file2 file1

The Following User Says Thank You to pamu For This Useful Post:
panyam (02-28-2013)
    #4  
Old Unix and Linux 03-01-2013
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 11 August 2015, 10:09 PM EDT
Location: Beijing China
Posts: 1,297
Thanks: 0
Thanked 24 Times in 24 Posts
awk, perl, python

awk

Code:
gawk -F"[|]" '{
if(NR==FNR){
  _[$1]=sprintf("%s,%s",_[$1],$2)
}
else{
  tmp=_[$1]
  n=split(tmp,arr,",")
  for(i=2;i<=n;i++){
    print $0"|"arr[i]
}
}
}
' b a

perl

Code:
open my $fh1,"<a.txt" or die "can not open a.txt";
open my $fh2,"<b.txt" or die "can not open b.txt";
my %hash;
while(<$fh2>){
	chomp;
	my @tmp = split("[|]",$_);
	push @{$hash{$tmp[0]}}, $tmp[1];
}
close $fh2;
while(<$fh1>){
	chomp;
	my @tmp = split("[|]", $_);
	my @tmp = @{$hash{$tmp[0]}};
	for(my $i=0;$i<=$#tmp;$i++){
		print $_,"|",$tmp[$i],"\n";
	}
}
close $fh1;

python

Code:
dict={}
with open("b.txt","r") as f1:
 for line in f1:
  s=line.replace("\n","")
  items=s.split("|")
  if items[0] in dict:
   dict[items[0]].append(items[1])
  else:
   dict[items[0]]=[items[1]]
with open("a.txt","r") as f2:
 for line in f2:
  s=line.replace("\n","")
  items=s.split("|")
  for i in dict[items[0]]:
   print(s,"|",i,sep="")

The Following User Says Thank You to summer_cherry For This Useful Post:
panyam (03-01-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
merging two files johnkim0806 Shell Programming and Scripting 2 06-26-2012 11:46 AM
Merging two files with same name jaysean Shell Programming and Scripting 5 06-18-2010 02:32 PM
Merging two files siba.s.nayak UNIX for Dummies Questions & Answers 4 12-23-2009 11:58 AM
merging of files. clx Shell Programming and Scripting 5 05-21-2009 02:00 PM
merging two files rameshonline Shell Programming and Scripting 14 04-06-2009 01:20 AM



All times are GMT -4. The time now is 01:50 AM.