# awk command hash array not printing

**URL:** <https://community.unix.com/t/awk-command-hash-array-not-printing/356701>\
**Category:** Shell Programming and Scripting\
**Created:** [November 11, 2015, 10:38pm UTC](https://community.unix.com/t/awk-command-hash-array-not-printing/356701 "2015-11-11T22:38:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanj123](https://community.unix.com/letter_avatar/sanj123/32/6_22734026db12803ebb3e059622ac3534.png) [@Sanj123](https://community.unix.com/u/Sanj123)\
**Post date:** [November 11, 2015, 10:38pm UTC](https://community.unix.com/t/awk-command-hash-array-not-printing/356701/1 "2015-11-11T22:38:51Z")

</div>

I'm new to awk command.

The HASH ARRAY is not printing. Merging 2 files togather.

vault\_input.txt

```nohighlight
3P04_Dep_Inxml:2230
REM02_Dep_Inxml:2200
REM03_Dep_Inxml:2400
REM05:2200
REM06:2200

```

tst6.txt

```nohighlight
Nov:10:2115:3P04_Dep_Inxml
Nov:10:2129:REM02_Dep_Inxml
Nov:10:2235:REM03_Dep_Inxml

```

using following command:

```nohighlight
awk 'NR==FNR{a[$1]=$2;next} $4 in a {print $4 a[$1] $3}' FS=":" vault_input.txt tst6.txt 

```

OUTPUT  
3P04\_Dep\_Inxml2115

```nohighlight
REM02_Dep_Inxml2129
REM03_Dep_Inxml2235

```

DESIRED RESULT

```nohighlight
3P04_Dep_Inxml 2230 2115
REM02_Dep_Inxml 2200 2129
REM03_Dep_Inxml 2400 2235

```

Looks like "$4 in a" is extracting correct info but nothing is getting populated  
in a[1].

THanks

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [November 11, 2015, 10:49pm UTC](https://community.unix.com/t/awk-command-hash-array-not-printing/356701/2 "2015-11-11T22:49:27Z")

</div>

```nohighlight
awk 'NR==FNR{a[$1]=$2;next} $4 in a {print $4, a[$4], $3}' FS=":" vault_input.txt tst6.txt

```

or:

```nohighlight
awk 'NR==FNR{a[$1]=$1OFS$2;next} $4 in a {print a[$4], $3}' FS=":" vault_input.txt tst6.txt

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [November 11, 2015, 10:49pm UTC](https://community.unix.com/t/awk-command-hash-array-not-printing/356701/3 "2015-11-11T22:49:57Z")

</div>

Try:

```nohighlight
print $4,a[$4],$3

```

---

<div class="post-metadata">

**Author:** ![Sanj123](https://community.unix.com/letter_avatar/sanj123/32/6_22734026db12803ebb3e059622ac3534.png) [@Sanj123](https://community.unix.com/u/Sanj123)\
**Post date:** [November 11, 2015, 11:06pm UTC](https://community.unix.com/t/awk-command-hash-array-not-printing/356701/4 "2015-11-11T23:06:56Z")

</div>

THankyou aia thankyou...it works

---------- Post updated at 11:06 PM ---------- Previous update was at 10:54 PM ----------

Aia

Thanks couild you expalin how (print $4, a[$4], $3) this works and not {print $4 a[$1] $3}  
This will help me understand for future refrences.

Thanks so much..i've been at this for 3 days

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [November 12, 2015, 12:23am UTC](https://community.unix.com/t/awk-command-hash-array-not-printing/356701/5 "2015-11-12T00:23:22Z")

</div>

`$1` from `vault_input.txt` is the same that `$4` from `tst6.txt` , that value is the index name for that associative array `a`

Example for the first line of `vault_input.txt` :

`a[$1]=$2` would be `a["3P04_Dep_Inxml"] = "2230"` , therefore to obtain the value back you must use the index name `3P04_Dep_Inxml` which is `$4` for file `tst6.txt`

---------- Post updated at 10:23 PM ---------- Previous update was at 10:17 PM ----------

When you get to this point:

```nohighlight
$4 in a {print a[$4], $3}

```

You are processing lines from file `tst6.txt` , so `$1` is different that `$1` from file `vault_input.txt`

---

<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:** [November 12, 2015, 12:50am UTC](https://community.unix.com/t/awk-command-hash-array-not-printing/356701/6 "2015-11-12T00:50:07Z")

</div>

Expanding on what Aia has already said... When you're reading `vault_input.txt` , using `$1` as your index in the array `a` you are setting:

```nohighlight
a["3P04_Dep_Inxml"] = 2230
a["REM02_Dep_Inxml"] = 2200
a["REM03_Dep_Inxml"] = 2400
a["REM05"] = 2200
a["REM06"] = 2200

```

and while you are reading `tst6.txt` , `$1` always expands to `"Nov"` and `a["Nov"]` has never been set. Therefore, `a[$1]` expands to an empty string while `a[$4]` expands to the corresponding value that was set while reading `vault_input.txt` .
