# left join using awk

**URL:** <https://community.unix.com/t/left-join-using-awk/312316>\
**Category:** Shell Programming and Scripting\
**Created:** [June 21, 2012, 6:00pm UTC](https://community.unix.com/t/left-join-using-awk/312316 "2012-06-21T18:00:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g1org1o](https://community.unix.com/letter_avatar/g1org1o/32/6_22734026db12803ebb3e059622ac3534.png) [@g1org1o](https://community.unix.com/u/g1org1o)\
**Post date:** [June 21, 2012, 6:00pm UTC](https://community.unix.com/t/left-join-using-awk/312316/1 "2012-06-21T18:00:34Z")

</div>

Hi guys,

I need to use awk to join 2 files

file\_1  
A 001  
B 002  
C 003

file\_2  
A XX1  
B XX2

output desired  
A 001 XX1  
B 002 missing  
C 003 XX2

thank you!

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_22734026db12803ebb3e059622ac3534.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [June 21, 2012, 6:48pm UTC](https://community.unix.com/t/left-join-using-awk/312316/2 "2012-06-21T18:48:32Z")

</div>

Didn't you mean that file\_2 contains :

```nohighlight
A XX1
C XX2

```

???

So it could be something like :

```nohighlight
awk 'NR==FNR{A[$1]=$2;next}{print$0 FS (A[$1]?A[$1]:"missing")}' file_2 file_1

```

Otherwise i don't get the logic of your output

---

<div class="post-metadata">

**Author:** ![g1org1o](https://community.unix.com/letter_avatar/g1org1o/32/6_22734026db12803ebb3e059622ac3534.png) [@g1org1o](https://community.unix.com/u/g1org1o)\
**Post date:** [June 21, 2012, 8:57pm UTC](https://community.unix.com/t/left-join-using-awk/312316/3 "2012-06-21T20:57:34Z")

</div>

Hi ctsgnb

I was wrong.. I meant

file\_1  
A 001  
B 002  
C 003

file\_2  
A XX1  
B XX2

output desired  
A 001 XX1  
B 002 XX2  
C 003 missing

anyway your awk script works! Thank you!
