Skip to main content
deleted 11 characters in body; edited title
Source Link
Michael Mrozek
  • 95.7k
  • 40
  • 245
  • 236

Why isn't this awk command doing a full outherouter join?

Objective  : Merge the contents of two files using common key present in the files

 file1.txt
 =========
 key1   11
 key2   12
 key3   13


 file2.txt
 =========
 key2   22
 key3   23
 key4   24
 key5   25


 Expected Output :
 ==================
 key1   11
 key2   12    22
 key3   13    23 
 key4   24
 key5   25

Approaches Tried tried:

  1. Tried joinjoin command:

     join -a 1 -a 2 file1.txt file2.txt ## full outer join
    
  2. Using awkawk:

     awk 'FNR==NR{a[$1]=$2;next;}{ print $0, a[$1]}' 2.txt 1.txt
    

Approach 2 is resulting in a right outer join and NOT a full outer join:

   key1  11
   key2  12    22
   key3  13    23 
 

What needs to be modified in approach 2 which willto result in a full outer join?

Why isn't this awk command doing a full outher join?

Objective  : Merge the contents of two files using common key present in the files

 file1.txt
 =========
 key1   11
 key2   12
 key3   13


 file2.txt
 =========
 key2   22
 key3   23
 key4   24
 key5   25


 Expected Output :
 ==================
 key1   11
 key2   12    22
 key3   13    23 
 key4   24
 key5   25

Approaches Tried :

  1. Tried join command

     join -a 1 -a 2 file1.txt file2.txt ## full outer join
    
  2. Using awk

     awk 'FNR==NR{a[$1]=$2;next;}{ print $0, a[$1]}' 2.txt 1.txt
    

Approach 2 is resulting in right outer join and NOT full outer join

   key1  11
   key2  12    22
   key3  13    23 
 

What needs to be modified in approach 2 which will result in full outer join?

Why isn't this awk command doing a full outer join?

Objective: Merge the contents of two files using common key present in the files

 file1.txt
 =========
 key1   11
 key2   12
 key3   13


 file2.txt
 =========
 key2   22
 key3   23
 key4   24
 key5   25


 Expected Output :
 ==================
 key1   11
 key2   12    22
 key3   13    23 
 key4   24
 key5   25

Approaches tried:

  1. join command:

     join -a 1 -a 2 file1.txt file2.txt ## full outer join
    
  2. awk:

     awk 'FNR==NR{a[$1]=$2;next;}{ print $0, a[$1]}' 2.txt 1.txt
    

Approach 2 is resulting in a right outer join and NOT a full outer join:

   key1  11
   key2  12    22
   key3  13    23 
 

What needs to be modified in approach 2 to result in a full outer join?

Post Merged (destination) from unix.stackexchange.com/questions/195061/…
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k
deleted 210 characters in body; edited tags; edited title
Source Link
Braiam
  • 36.9k
  • 29
  • 114
  • 176

Merge two files using JOIN or AWK Why isn't this awk command doing a full outher join?

Objective : Merge the contents of two files using common key present in the files

 file1.txt
 =========
 key1   11
 key2   12
 key3   13


 file2.txt
 =========
 key2   22
 key3   23
 key4   24
 key5   25


 Expected Output :
 ==================
 key1   11
 key2   12    22
 key3   13    23 
 key4   24
 key5   25

Approaches Tried :

  1. Tried join command

     join -a 1 -a 2 file1.txt file2.txt ## full outer join
    
  2. Using awk

     awk 'FNR==NR{a[$1]=$2;next;}{ print $0, a[$1]}' 2.txt 1.txt
    

Approach 2 is resulting in right outer join and NOT full outer join

   key1  11
   key2  12    22
   key3  13    23 
 

Questions :

  1. What needs to be modified in approach 2 which will result in full outer join ?
  2. Which would be the better approach (#1 or #2) when we have lot of key-value pairs in files (file size of GB/TB)

Any inputs/ thoughts on the above questions is much appreciated thanks.What needs to be modified in approach 2 which will result in full outer join?

Merge two files using JOIN or AWK

Objective : Merge the contents of two files using common key present in the files

 file1.txt
 =========
 key1   11
 key2   12
 key3   13


 file2.txt
 =========
 key2   22
 key3   23
 key4   24
 key5   25


 Expected Output :
 ==================
 key1   11
 key2   12    22
 key3   13    23 
 key4   24
 key5   25

Approaches Tried :

  1. Tried join command

     join -a 1 -a 2 file1.txt file2.txt ## full outer join
    
  2. Using awk

     awk 'FNR==NR{a[$1]=$2;next;}{ print $0, a[$1]}' 2.txt 1.txt
    

Approach 2 is resulting in right outer join and NOT full outer join

   key1  11
   key2  12    22
   key3  13    23 
 

Questions :

  1. What needs to be modified in approach 2 which will result in full outer join ?
  2. Which would be the better approach (#1 or #2) when we have lot of key-value pairs in files (file size of GB/TB)

Any inputs/ thoughts on the above questions is much appreciated thanks.

Why isn't this awk command doing a full outher join?

Objective : Merge the contents of two files using common key present in the files

 file1.txt
 =========
 key1   11
 key2   12
 key3   13


 file2.txt
 =========
 key2   22
 key3   23
 key4   24
 key5   25


 Expected Output :
 ==================
 key1   11
 key2   12    22
 key3   13    23 
 key4   24
 key5   25

Approaches Tried :

  1. Tried join command

     join -a 1 -a 2 file1.txt file2.txt ## full outer join
    
  2. Using awk

     awk 'FNR==NR{a[$1]=$2;next;}{ print $0, a[$1]}' 2.txt 1.txt
    

Approach 2 is resulting in right outer join and NOT full outer join

   key1  11
   key2  12    22
   key3  13    23 
 

What needs to be modified in approach 2 which will result in full outer join?

added 2 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718
Loading
edited body
Source Link
Loading
Source Link
Loading