# AWK pattern matching on loop

**URL:** <https://community.unix.com/t/awk-pattern-matching-on-loop/310755>\
**Category:** Shell Programming and Scripting\
**Created:** [May 24, 2012, 7:04am UTC](https://community.unix.com/t/awk-pattern-matching-on-loop/310755 "2012-05-24T07:04:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![howielim](https://community.unix.com/letter_avatar/howielim/32/6_449c533f586939b6e5f246632a46421f.png) [@howielim](https://community.unix.com/u/howielim)\
**Post date:** [May 24, 2012, 7:04am UTC](https://community.unix.com/t/awk-pattern-matching-on-loop/310755/1 "2012-05-24T07:04:46Z")

</div>

Hi,

I am still a beginner on shell scripting so please bear with me.

What i am trying to do is filter my logfile based on some ID on field 24 which is defined in array. The filter result output will be moved to my log folder with the same name. The problem is when not using loop, this command can work.

```nohighlight
awk '$24 == 32596 ' program/application/logs/archive/click/$x >> log/$x

```

But when using loop, my script wont work. Please notice the bold variable.

```nohighlight
id=(32596 32595 32594 32589 32580 32579); for x in `ls`;do for y in ${id[@]}; do awk '$24 == $y ' program/application/logs/archive/click/$x >> log/$x;done;done

```

Is there something i must or change to get this script working?

Thanks before for answering.

---

<div class="post-metadata">

**Author:** ![guruprasadpr](https://community.unix.com/user_avatar/community.unix.com/guruprasadpr/32/1542_2.png) [@guruprasadpr](https://community.unix.com/u/guruprasadpr)\
**Post date:** [May 24, 2012, 7:08am UTC](https://community.unix.com/t/awk-pattern-matching-on-loop/310755/2 "2012-05-24T07:08:13Z")

</div>

Hi

```nohighlight
awk -v y=$y '$24 == y' program/application......

```

---

<div class="post-metadata">

**Author:** ![howielim](https://community.unix.com/letter_avatar/howielim/32/6_449c533f586939b6e5f246632a46421f.png) [@howielim](https://community.unix.com/u/howielim)\
**Post date:** [May 24, 2012, 9:45pm UTC](https://community.unix.com/t/awk-pattern-matching-on-loop/310755/3 "2012-05-24T21:45:20Z")

</div>

> [@guruprasadpr](#):
>
> Hi
> 
> ```plaintext
> awk -v y=$y '$24 == y' program/application......
> 
> ```

Works like a charm. Thanks
