# Pattern Search using AWK

**URL:** <https://community.unix.com/t/pattern-search-using-awk/290099>\
**Category:** Shell Programming and Scripting\
**Created:** [June 20, 2011, 3:33am UTC](https://community.unix.com/t/pattern-search-using-awk/290099 "2011-06-20T03:33:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajaincv](https://community.unix.com/letter_avatar/ajaincv/32/6_22734026db12803ebb3e059622ac3534.png) [@ajaincv](https://community.unix.com/u/ajaincv)\
**Post date:** [June 20, 2011, 3:33am UTC](https://community.unix.com/t/pattern-search-using-awk/290099/1 "2011-06-20T03:33:01Z")

</div>

Hi All,

I have the below file data.txt.Using awk i want to grep all the zone data.Form the below command i can extact data upto of zone i give but i want it should print until next pattern.

```nohighlight
awk '/^Total Collection /{c=5;next}c-->0' zin45srs08.tools_utilization

```

instead of c=5 is it possible to give next pattern.

##########################

```nohighlight
cat data.txt
Total Collection = $10291 {Fri May 8}
zone7 4500
zone8 3545
zone1 1200
zone0 900
Total Collection = $11847 {Sat May 9}
zone1 2800
zone3 2800
zone6 2567
zone8 2300
zone9 1200
zone12 90
zone11 90

```

############################

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [June 20, 2011, 3:50am UTC](https://community.unix.com/t/pattern-search-using-awk/290099/2 "2011-06-20T03:50:14Z")

</div>

If you want to print all the zones:

```nohighlight
grep "^zone" file

```

To print a specific collection (i.e. the first one) you can do something like:

```nohighlight
awk '/^Total Collection/ && /\$10291/ {c=1;next}c && /^Total Collection/{c=0}c' file

```

---

<div class="post-metadata">

**Author:** ![ajaincv](https://community.unix.com/letter_avatar/ajaincv/32/6_22734026db12803ebb3e059622ac3534.png) [@ajaincv](https://community.unix.com/u/ajaincv)\
**Post date:** [June 20, 2011, 3:55am UTC](https://community.unix.com/t/pattern-search-using-awk/290099/3 "2011-06-20T03:55:56Z")

</div>

Hi Franklin,

Thanks for your reply.Query is, i am searching on Total collection so need to print all the information between them.Total collection may be n & my zones will also vary.

Regards

Ankit

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [June 20, 2011, 4:05am UTC](https://community.unix.com/t/pattern-search-using-awk/290099/4 "2011-06-20T04:05:09Z")

</div>

```nohighlight
 
nawk ' BEGIN {FS="Total\ Collection"} {print $1}' /tmp/myfile

```

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [June 20, 2011, 4:11am UTC](https://community.unix.com/t/pattern-search-using-awk/290099/5 "2011-06-20T04:11:18Z")

</div>

Can you explain what's wrong with the solutions above?

If you don't get the correct output, post a better input file and the desired output.

---

<div class="post-metadata">

**Author:** ![ajaincv](https://community.unix.com/letter_avatar/ajaincv/32/6_22734026db12803ebb3e059622ac3534.png) [@ajaincv](https://community.unix.com/u/ajaincv)\
**Post date:** [June 20, 2011, 4:13am UTC](https://community.unix.com/t/pattern-search-using-awk/290099/6 "2011-06-20T04:13:21Z")

</div>

Firsly nawk is not running on my system & if i m using without nawk its print all information.

Regards

Ankit
