# Pattern mapping

**URL:** <https://community.unix.com/t/pattern-mapping/274366>\
**Category:** Shell Programming and Scripting\
**Created:** [September 29, 2010, 6:31am UTC](https://community.unix.com/t/pattern-mapping/274366 "2010-09-29T06:31:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danish\_Shakil](https://community.unix.com/letter_avatar/danish_shakil/32/6_449c533f586939b6e5f246632a46421f.png) [@Danish\_Shakil](https://community.unix.com/u/Danish_Shakil)\
**Post date:** [September 29, 2010, 6:31am UTC](https://community.unix.com/t/pattern-mapping/274366/1 "2010-09-29T06:31:36Z")

</div>

Dear Friends,

Please help me on this  
I have file A.txt containing text lines as below

```auto
grectec; 30 ,50, 60, base_123 ; top09
grectec; 30 ,55, 60, base_123 ; top09  
grectec; 10 ,53, 60, base_123 ; top09
grectec; 50 ,57, 60, base_123 ; top09
...
...

```

another file B.txt containing test lines as below

```auto
10 ken
30 jack
50 sack
60 mug

```

I need a script that check if the line containing text 30 add ken at the end of the line, similarly if the line containing 10 add ken at the end of the line.

output should be as: C.txt

grectec; 30 ,50, 60, base\_123 ; top09 jack  
grectec; 30 ,55, 60, base\_123 ; top09 jack  
grectec; 10 ,53, 60, base\_123 ; top09 ken  
grectec; 50 ,57, 60, base\_123 ; top09 sack

I think its would be done using join command but dont know how:

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [September 29, 2010, 6:47am UTC](https://community.unix.com/t/pattern-mapping/274366/2 "2010-09-29T06:47:56Z")

</div>

```nohighlight
awk > C.txt 'NR == FNR {
  b[$1] = $2; next
  }
$0 = $0 FS b[$2]
  ' B.txt A.txt

```

---

<div class="post-metadata">

**Author:** ![malikshahid85](https://community.unix.com/letter_avatar/malikshahid85/32/6_449c533f586939b6e5f246632a46421f.png) [@malikshahid85](https://community.unix.com/u/malikshahid85)\
**Post date:** [September 29, 2010, 9:23am UTC](https://community.unix.com/t/pattern-mapping/274366/3 "2010-09-29T09:23:37Z")

</div>

> [@danish shakil](#):
>
> Dear Friends,
> 
> Please help me on this  
> I have file A.txt containing text lines as below
> 
> ```html
> grectec; 30 ,50, 60, base_123 ; top09
> grectec; 30 ,55, 60, base_123 ; top09  
> grectec; 10 ,53, 60, base_123 ; top09
> grectec; 50 ,57, 60, base_123 ; top09
> ...
> ...
> 
> ```
> 
> another file B.txt containing test lines as below
> 
> ```html
> 10 ken
> 30 jack
> 50 sack
> 60 mug
> 
> ```
> 
> I need a script that check if the line containing text 30 add ken at the end of the line, similarly if the line containing 10 add ken at the end of the line.
> 
> output should be as: C.txt
> 
> grectec; 30 ,50, 60, base\_123 ; top09 jack  
> grectec; 30 ,55, 60, base\_123 ; top09 jack  
> grectec; 10 ,53, 60, base\_123 ; top09 ken  
> grectec; 50 ,57, 60, base\_123 ; top09 sack
> 
> I think its would be done using join command but dont know how:

Below code works exactly as you want.but a.txt should be like  
A.txt.

```plaintext
grectec; 10 ,53, 60, base_123 ; top09
grectec; 30 ,50, 60, base_123 ; top09
grectec; 30 ,55, 60, base_123 ; top09
grectec; 50 ,57, 60, base_123 ; top09

```

```plaintext
 
join -j1 2 -j2 1 a.txt b.txt
 

```

---

<div class="post-metadata">

**Author:** ![Danish\_Shakil](https://community.unix.com/letter_avatar/danish_shakil/32/6_449c533f586939b6e5f246632a46421f.png) [@Danish\_Shakil](https://community.unix.com/u/Danish_Shakil)\
**Post date:** [September 30, 2010, 4:25am UTC](https://community.unix.com/t/pattern-mapping/274366/4 "2010-09-30T04:25:38Z")

</div>

Thanks Radoulov,

It worked, i need a little more help:

```auto
File a.txt 
1 1 bash enable unlocked
1 4 kash enable locked
1 8 gash enable unlocked
2 1 bash enable unlocked
2 3 Lash enable unlocked 
2 7 Pash enable unlocked

```

```auto
another File b.txt

1 1 jack
1 2 yack
1 4 back
1 8 sack
2 1 pack
2 3 fack
2 4 mack
2 7 lack

```

i need a third file c.txt on a mapping criteria of first two columns

1 1 jack bash enable unlocked  
1 4 back kash enable locked  
1 8 sack gash enable unlocked  
2 1 pack bash enable unlocked  
2 3 fack Lash enable unlocked  
2 7 lack Pash enable unlocked

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [September 30, 2010, 4:29am UTC](https://community.unix.com/t/pattern-mapping/274366/5 "2010-09-30T04:29:35Z")

</div>

```nohighlight
awk 'NR == FNR {
  key[$1, $2] = $3; next
  }
$2 = $2 OFS key[$1, $2]
  ' OFS=\\t b.txt a.txt

```
