# using nawk

**URL:** https://community.unix.com/t/using-nawk/282414
**Category:** Shell Programming and Scripting
**Created:** [February 8, 2011, 8:28am UTC](https://community.unix.com/t/using-nawk/282414 "2011-02-08T08:28:11Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Diddy](https://community.unix.com/letter_avatar/diddy/32/5_5575768a8748004e209b776fc1b2916d.png) [@Diddy](https://community.unix.com/u/Diddy)
#### Post date: [February 8, 2011, 8:28am UTC](https://community.unix.com/t/using-nawk/282414/1 "2011-02-08T08:28:11Z")

</div>

help out with code. two files aaa bbb contains some records..output file xyz should be like this..see below  
i/p file:aaa

08350|60521|0000|505|0000|1555|000|NYCMT|Pd\_1 |-11878

i/p file: bbb

60521|60510

o/p file :xyz

60510|08350|60521|0000|505|0000|1555|000|NYCMT|Pd\_1 |-11878

---

<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: [February 8, 2011, 8:37am UTC](https://community.unix.com/t/using-nawk/282414/2 "2011-02-08T08:37:37Z")

</div>

```nohighlight
nawk -F'|' 'FNR==NR{f2[$1]=$2;next} $2 in f2 {$1=f2[$2] OFS $1}1' OFS='|' bbb aaa > xyz

```

---

<div class="post-metadata">

### Author: ![Diddy](https://community.unix.com/letter_avatar/diddy/32/5_5575768a8748004e209b776fc1b2916d.png) [@Diddy](https://community.unix.com/u/Diddy)
#### Post date: [February 8, 2011, 8:59am UTC](https://community.unix.com/t/using-nawk/282414/3 "2011-02-08T08:59:31Z")

</div>

its also printing record in aaa. like this

output:

```nohighlight
08350|60321|0000|505|0000|1555|000|NYCMT|Pd_1 |-3023.38
60510|08350|60521|0000|505|0000|1555|000|NYCMT|Pd_1 |-11878|

```

---

<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: [February 8, 2011, 9:34am UTC](https://community.unix.com/t/using-nawk/282414/4 "2011-02-08T09:34:09Z")

</div>

> [@diddy](#):
>
> its also printing record in aaa. like this
> 
> output:
> 
> ```plaintext
> 08350|60321|0000|505|0000|1555|000|NYCMT|Pd_1 |-3023.38
> 60510|08350|60521|0000|505|0000|1555|000|NYCMT|Pd_1 |-11878|
> 
> ```

Had you given a more representative sample AND a desired output based on this sample, we could have provided a better solution....  
Once again - guessing what you're after....

```plaintext
nawk -F'|' 'FNR==NR{f2[$1]=$2;next} $2 in f2 {$1=f2[$2] OFS $1;print}' OFS='|' bbb aaa > xyz

```

---

<div class="post-metadata">

### Author: ![yinyuemi](https://community.unix.com/letter_avatar/yinyuemi/32/5_5575768a8748004e209b776fc1b2916d.png) [@yinyuemi](https://community.unix.com/u/yinyuemi)
#### Post date: [February 8, 2011, 1:20pm UTC](https://community.unix.com/t/using-nawk/282414/5 "2011-02-08T13:20:08Z")

</div>

```nohighlight
awk 'BEGIN{FS=OFS="|"}{a[$1]=$2}NR>FNR && a[$2] {print a[$2],$0}' bbb aaa

```

---

<div class="post-metadata">

### Author: ![Diddy](https://community.unix.com/letter_avatar/diddy/32/5_5575768a8748004e209b776fc1b2916d.png) [@Diddy](https://community.unix.com/u/Diddy)
#### Post date: [February 9, 2011, 1:05am UTC](https://community.unix.com/t/using-nawk/282414/6 "2011-02-09T01:05:39Z")

</div>

thanks [![](http://linux.unix.com/customavatars/avatar37898_1.gif)](http://www.unix.com/members/37898.html)[vgersh99](http://www.unix.com/members/37898.html) n [yinyuemi](http://www.unix.com/members/302090799.html)
