Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-28-2013
saj saj is offline
Registered User
 
Join Date: Apr 2011
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 02-28-2013
Registered User
 
Join Date: Jul 2012
Location: Chennai
Posts: 507
Thanks: 37
Thanked 65 Times in 63 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
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,546
Thanks: 57
Thanked 448 Times in 444 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
Registered User
 
Join Date: Jun 2007
Location: Beijing China
Posts: 1,277
Thanks: 0
Thanked 20 Times in 20 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
Reply

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 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 02:57 PM.