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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
join two files koti_rama Shell Programming and Scripting 5 08-05-2008 01:20 AM
Join two files koti_rama Shell Programming and Scripting 4 06-10-2008 03:15 AM
how to join files jxh461 UNIX for Dummies Questions & Answers 5 08-23-2007 04:11 AM
join files mohan705 Shell Programming and Scripting 3 03-15-2007 02:51 AM
Join Files choppas Shell Programming and Scripting 2 10-18-2006 07:03 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-10-2008
Registered User
 

Join Date: Mar 2008
Posts: 17
Stumble this Post!
Fullouter Join of two files.

Hello All,

I have two files with following data.

File1:

1,AAA
2,BBB
3,CCC
4,DDD

File2:
1,XXX
2,YYY
3,ZZZ
5,EEE
6,QQQ


Expected Output Should be:

1,AAA,XXX
2,BBB,YYY
3,CCC,ZZZ
4,DDD,
5, ,EEE
6, ,QQQ

I tried it using awk command, but I could get only LeftOuter join.

Please suggest.

Thanks in advance.

Amit
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-10-2008
Registered User
 

Join Date: Apr 2008
Location: Bangalore
Posts: 120
Stumble this Post!
Hi


awk -F"," ' NR == FNR { a[$1]=$2 ; next}
{print $1","a[$1]","$2}' file1 file2



Thanks
Penchal
Reply With Quote
  #3 (permalink)  
Old 06-10-2008
Registered User
 

Join Date: Mar 2008
Posts: 17
Stumble this Post!
Thanks for replying Penchal.

Your command is giving me the output as below...

1,AAA,XXX
2,BBB,YYY
3,CCC,ZZZ
5,,EEE
6,,QQQ
,,

I am getting all the records from file2 appended with the corrosponding values in file1. But i do not get the records present in file1 and not in file2.
Reply With Quote
  #4 (permalink)  
Old 06-10-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,851
Stumble this Post!
try the paste command.
Reply With Quote
  #5 (permalink)  
Old 06-10-2008
Registered User
 

Join Date: Apr 2008
Location: Bangalore
Posts: 120
Stumble this Post!
Try the following script

awk -F"," ' NR == FNR { a[$1]=$2 ; next}
{print $1","a[$1]","$2}' file1 file2 >> temp1

awk -F"," ' NR == FNR { a[$1]=$2 ; next}
{print $1","$2","a[$1]}' file1 file2 >> temp2

cat temp1 temp2 >> final_test

sort -u final_test



This is working fine at my end.

Thanks
Penchal
Reply With Quote
  #6 (permalink)  
Old 06-10-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,538
Stumble this Post!
This may give you an idea:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk -F, 'END { 
  for (i=1; i<=m; i++) 
    print i, _[i] 
    }
{ 
  _[$1] = _[$1] ? _[$1] FS $2 : NR == FNR ? $2 : FS $2
  if ($1 > m) m = $1 
  }' OFS=, file1 file2
Reply With Quote
  #7 (permalink)  
Old 06-10-2008
Registered User
 

Join Date: Mar 2008
Posts: 17
Stumble this Post!
Thumbs up

Thanks Penchal.

Its working Now.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
solaris

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0