# Search for patterns on different lines

**URL:** <https://community.unix.com/t/search-for-patterns-on-different-lines/365724>\
**Category:** Shell Programming and Scripting\
**Created:** [March 23, 2017, 12:44am UTC](https://community.unix.com/t/search-for-patterns-on-different-lines/365724 "2017-03-23T00:44:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [March 23, 2017, 12:44am UTC](https://community.unix.com/t/search-for-patterns-on-different-lines/365724/1 "2017-03-23T00:44:48Z")

</div>

im using the following code to search a log for entries on two different lines:

```nohighlight
awk 'BEGIN{count=0} /'"${firstpattern}"'/,/'"${secondpattern}"'/ { print; if ($0 ~ /'"${thirdpattern}"'/){count++}; } END { print count }' data.txt

```

```nohighlight
firstpattern="start error log"
secondpattern="i am logging the errors now"
thirdpattern="App crashed"

```

data.txt

```nohighlight
blahblahblahblahahahahahahahahahahahlalalalalalalalalal
auffkffiaooojf jafkaf kafkakf akkkkkkkkkkkfaf kafkafk akfkafka
March 22 2017 start error log
today is lovely. it is not raining. No snow. fantastic
......
I am logging the errors now
tomorrow. it will rain. stay in doors.
App crashed
.......
I am fine today. Just dont bother me.  
Im watching a movie tomorrow. You;re not invited.

```

Now, when the above awk code is run on the data.txt file, it will pull out only the text im interested in, which is:

```nohighlight
March 22 2017 start error log
today is lovely. it is not raining. No snow. fantastic
......
I am logging the errors now
tomorrow. it will rain. stay in doors.
App crashed

```

sometimes, there will be many instances where multiple "start error log" are present. but neither "I am logging the errors now" nor "App crashed" strings will be present. there will be other errors in their place, errors i dont care about. in these instances, the errors that I do care about "I am logging the errors now" and "App crashed" are usually towards the bottom under one of the other "start error log" strings.

so what ends up happening is that, the awk code tries to print everything it finds from the first "start error log" instance, to the last.

i want the awk code to skip printing an entire chunk of lines if the three patterns im looking for are not all there.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [March 23, 2017, 1:33am UTC](https://community.unix.com/t/search-for-patterns-on-different-lines/365724/2 "2017-03-23T01:33:08Z")

</div>

It seems in your script have pattern 2 and 3 reversed, no?  
If so, try something like this:

```nohighlight
awk -v p1="start error log" -v p2="I am logging the errors now" -v p3="App crashed" '
  s!="" {
    s=s RS $0
    if($0~p3) {
      if (s~p2)
        print s
      s=""
    }
  } 
  $0~p1 {
    s=$0
  }
' data.txt

```

---  
Also in you sample secondpattern variable, the "I" was written as "i"
