Merging two files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Merging two files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 08:21 AM..
Sponsored Links
    #2  
Old 02-28-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 10 July 2014, 12:57 PM EDT
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 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 02-28-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
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 03-01-2013
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 23 July 2014, 3:17 AM EDT
Location: Beijing China
Posts: 1,294
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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 12:46 PM
Merging two files with same name jaysean Shell Programming and Scripting 5 06-18-2010 03:32 PM
Merging two files siba.s.nayak UNIX for Dummies Questions & Answers 4 12-23-2009 12:58 PM
merging of files. clx Shell Programming and Scripting 5 05-21-2009 03:00 PM
merging two files rameshonline Shell Programming and Scripting 14 04-06-2009 02:20 AM



All times are GMT -4. The time now is 01:46 PM.