|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 .
Last edited by saj; 02-28-2013 at 07:21 AM.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 aperl 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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|