# Vlookup using awk without exact match for two colum input

**URL:** <https://community.unix.com/t/vlookup-using-awk-without-exact-match-for-two-colum-input/371808>\
**Category:** Shell Programming and Scripting\
**Created:** [April 16, 2018, 1:45pm UTC](https://community.unix.com/t/vlookup-using-awk-without-exact-match-for-two-colum-input/371808 "2018-04-16T13:45:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranjancom2000](https://community.unix.com/letter_avatar/ranjancom2000/32/6_449c533f586939b6e5f246632a46421f.png) [@ranjancom2000](https://community.unix.com/u/ranjancom2000)\
**Post date:** [April 16, 2018, 1:45pm UTC](https://community.unix.com/t/vlookup-using-awk-without-exact-match-for-two-colum-input/371808/1 "2018-04-16T13:45:40Z")

</div>

Source file 1

```nohighlight
335 R1-snapfound 0098F RDFType:R1 R2-Dev R2-snapfound ,010C0 RemoteSymmetrixID:345
335 R1-snapfound 00990 RDFType:R1 R2-Dev R2-snapfound ,010C1 RemoteSymmetrixID:345
335 R1-snapfound 009C0 RDFType:R1 R2-Dev R2-snapfound ,009C1 RemoteSymmetrixID:345
335 R1-snapfound 009C1 RDFType:R1 R2-Dev R2-snapfound ,009C2 RemoteSymmetrixID:345
335 R1-snapfound 009C2 RDFType:R1 R2-Dev R2-snapfound ,009C3 RemoteSymmetrixID:345

```

Source file 2

```nohighlight
335 server1_silver 0098F
335 server1_silver 00990
335 server1_silver 009C0
335 server1_silver 00ab4
335 server1_silver 00ab5
335 server1_silver 00ab6

```

It need to check for third column with each file and append second and third column in output file

Output required

```nohighlight
335 server1_silver 0098F R1-snapfound 0098F RDFType:R1 R2-Dev R2-snapfound ,010C0 RemoteSymmetrixID:345
335 server1_silver 00990 R1-snapfound 00990 RDFType:R1 R2-Dev R2-snapfound ,010C1 RemoteSymmetrixID:345
335 server1_silver 009C0 R1-snapfound 009C0 RDFType:R1 R2-Dev R2-snapfound ,009C1 RemoteSymmetrixID:345
335 R1-snapfound 009C1 RDFType:R1 R2-Dev R2-snapfound ,009C2 RemoteSymmetrixID:345
335 R1-snapfound 009C2 RDFType:R1 R2-Dev R2-snapfound ,009C3 RemoteSymmetrixID:345

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [April 16, 2018, 2:06pm UTC](https://community.unix.com/t/vlookup-using-awk-without-exact-match-for-two-colum-input/371808/2 "2018-04-16T14:06:53Z")

</div>

```nohighlight
 awk 'FNR==NR {f2[$3]=$0;next} {print (($3 in f2)?f2[$3] OFS:"") $0}' file2 file1

```

---

<div class="post-metadata">

**Author:** ![ranjancom2000](https://community.unix.com/letter_avatar/ranjancom2000/32/6_449c533f586939b6e5f246632a46421f.png) [@ranjancom2000](https://community.unix.com/u/ranjancom2000)\
**Post date:** [April 16, 2018, 3:03pm UTC](https://community.unix.com/t/vlookup-using-awk-without-exact-match-for-two-colum-input/371808/3 "2018-04-16T15:03:56Z")

</div>

> [@vgersh99](#):
>
> ```plaintext
> awk 'FNR==NR {f2[$3]=$0;next} {print (($3 in f2)?f2[$3] OFS:"") $0}' file2 file1
> 
> ```

this is working great. How to run if i want to use without case sensitive

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 16, 2018, 3:43pm UTC](https://community.unix.com/t/vlookup-using-awk-without-exact-match-for-two-colum-input/371808/4 "2018-04-16T15:43:27Z")

</div>

Try:

```nohighlight
 awk 'FNR==NR {f2[tolower($3)]=$0;next} {print ((tolower($3) in f2)?f2[tolower($3)] OFS:"") $0}' file2 file1

```

---

<div class="post-metadata">

**Author:** ![ranjancom2000](https://community.unix.com/letter_avatar/ranjancom2000/32/6_449c533f586939b6e5f246632a46421f.png) [@ranjancom2000](https://community.unix.com/u/ranjancom2000)\
**Post date:** [April 17, 2018, 6:40am UTC](https://community.unix.com/t/vlookup-using-awk-without-exact-match-for-two-colum-input/371808/5 "2018-04-17T06:40:54Z")

</div>

> [@don cragun](#):
>
> Try:
> 
> ```plaintext
> awk 'FNR==NR {f2[tolower($3)]=$0;next} {print ((tolower($3) in f2)?f2[tolower($3)] OFS:"") $0}' file2 file1
> 
> ```

Great it was working

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 17, 2018, 7:55am UTC](https://community.unix.com/t/vlookup-using-awk-without-exact-match-for-two-colum-input/371808/6 "2018-04-17T07:55:36Z")

</div>

As a non-existing element of `f2` will yield an empty string if referenced, you might simplify above to

```nohighlight
awk 'FNR==NR {f2[tolower($3)]=$0 OFS;next} {print f2[tolower($3)] $0}' file2 file1
335 server1_silver 0098F 335 R1-snapfound 0098F RDFType:R1 R2-Dev R2-snapfound ,010C0 RemoteSymmetrixID:345
335 server1_silver 00990 335 R1-snapfound 00990 RDFType:R1 R2-Dev R2-snapfound ,010C1 RemoteSymmetrixID:345
335 server1_silver 009C0 335 R1-snapfound 009C0 RDFType:R1 R2-Dev R2-snapfound ,009C1 RemoteSymmetrixID:345
335 R1-snapfound 009C1 RDFType:R1 R2-Dev R2-snapfound ,009C2 RemoteSymmetrixID:345
335 R1-snapfound 009C2 RDFType:R1 R2-Dev R2-snapfound ,009C3 RemoteSymmetrixID:345

```
