# Simple awk search problem

**URL:** <https://community.unix.com/t/simple-awk-search-problem/300718>\
**Category:** Shell Programming and Scripting\
**Created:** [December 7, 2011, 9:33pm UTC](https://community.unix.com/t/simple-awk-search-problem/300718 "2011-12-07T21:33:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![delphys](https://community.unix.com/letter_avatar/delphys/32/5_5575768a8748004e209b776fc1b2916d.png) [@delphys](https://community.unix.com/u/delphys)\
**Post date:** [December 7, 2011, 9:33pm UTC](https://community.unix.com/t/simple-awk-search-problem/300718/1 "2011-12-07T21:33:48Z")

</div>

Hello;

we have :

```nohighlight
awk '/reg_exp/,0/

```

prints every line after the first occurrence of "reg\_exp"

But if I want to print rest of the lines AFTER the last occurrence of "reg\_exp",  
how would I do it ??

Tried :

```nohighlight
awk ' ! (/reg_exp/,0)' 

```

But it errored...

Thank you for any suggestions..

---

<div class="post-metadata">

**Author:** ![mirni](https://community.unix.com/letter_avatar/mirni/32/5_5575768a8748004e209b776fc1b2916d.png) [@mirni](https://community.unix.com/u/mirni)\
**Post date:** [December 7, 2011, 10:01pm UTC](https://community.unix.com/t/simple-awk-search-problem/300718/2 "2011-12-07T22:01:40Z")

</div>

Well this

```nohighlight
awk '/reg_exp/,0

```

has some syntax errors and wouldn't run.

How about a naive approach:

```nohighlight
 awk '/regex/{flag=1;next}flag' input

```

EDIT: oops, I misread the part "after the last occurrence". Sorry

To find out that the occurrence is the last, awk has to search through the whole file. So you either need to scan through file twice (once in the BEGIN block, to find which is the last), or save the lines in an array, which may overflow your memory, if the file is really large. So I would make use of 'tac' which will print the lines in reverse order:

```nohighlight
 tac lines | sed -n '/six/{q};p' | tac

```

---

<div class="post-metadata">

**Author:** ![Ygor](https://community.unix.com/user_avatar/community.unix.com/ygor/32/104_2.png) [@Ygor](https://community.unix.com/u/Ygor)\
**Post date:** [December 7, 2011, 10:03pm UTC](https://community.unix.com/t/simple-awk-search-problem/300718/3 "2011-12-07T22:03:07Z")

</div>

Try...

```nohighlight
awk '/reg_exp/{x=NR}x{y[NR]=$0}END{if(x)for(z=x;z<=NR;z++)print y[z]}' file1

```

---

<div class="post-metadata">

**Author:** ![delphys](https://community.unix.com/letter_avatar/delphys/32/5_5575768a8748004e209b776fc1b2916d.png) [@delphys](https://community.unix.com/u/delphys)\
**Post date:** [December 8, 2011, 8:06am UTC](https://community.unix.com/t/simple-awk-search-problem/300718/4 "2011-12-08T08:06:52Z")

</div>

```nohighlight
awk '/reg_exp/{x=NR}x{y[NR]=$0}END{if(x)for(z=x;z<=NR;z++)print y[z]}' file1

```

Thank you very much for this solution ..

Not sure if I understand the logic in this code..

When you have a chance, may I get you to explain it ??

Thnx again

---

<div class="post-metadata">

**Author:** ![delphys](https://community.unix.com/letter_avatar/delphys/32/5_5575768a8748004e209b776fc1b2916d.png) [@delphys](https://community.unix.com/u/delphys)\
**Post date:** [December 9, 2011, 7:54am UTC](https://community.unix.com/t/simple-awk-search-problem/300718/5 "2011-12-09T07:54:04Z")

</div>

Thank you, Ygor ..

Trying to understand more complex awk syntax ..
