# How can I get the lines between two patterns?

**URL:** <https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225>\
**Category:** Shell Programming and Scripting\
**Created:** [October 13, 2009, 2:24pm UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225 "2009-10-13T14:24:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![JamesByars](https://community.unix.com/letter_avatar/jamesbyars/32/6_449c533f586939b6e5f246632a46421f.png) [@JamesByars](https://community.unix.com/u/JamesByars)\
**Post date:** [October 13, 2009, 2:24pm UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/1 "2009-10-13T14:24:05Z")

</div>

hi,

I have the following file

```nohighlight
hello
world
this
is 
to
say
bye
to
everyone
so
bye

```

I want to get the lines from hello to the first bye inclusive into another file?

how can I do this

thanks

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 13, 2009, 2:28pm UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/2 "2009-10-13T14:28:55Z")

</div>

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

````auto
```text
 and 
```

````

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 02:28 PM ---------- Previous update was at 02:26 PM ----------

```nohighlight
sed -n '/hello/,/bye/p' myFile

```

---

<div class="post-metadata">

**Author:** ![steadyonabix](https://community.unix.com/letter_avatar/steadyonabix/32/6_449c533f586939b6e5f246632a46421f.png) [@steadyonabix](https://community.unix.com/u/steadyonabix)\
**Post date:** [October 13, 2009, 2:38pm UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/3 "2009-10-13T14:38:20Z")

</div>

Don't do it youngster, learn a bit of awk, it will give your life new meaning 😉

```nohighlight
nawk '/bye/{print;exit} /hello/{p=1}p' infile > another.file

```

---

<div class="post-metadata">

**Author:** ![JamesByars](https://community.unix.com/letter_avatar/jamesbyars/32/6_449c533f586939b6e5f246632a46421f.png) [@JamesByars](https://community.unix.com/u/JamesByars)\
**Post date:** [October 19, 2009, 4:54pm UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/4 "2009-10-19T16:54:07Z")

</div>

> [@steadyonabix](#):
>
> Don't do it youngster, learn a bit of awk, it will give your life new meaning 😉
> 
> ```plaintext
> nawk '/bye/{print;exit} /hello/{p=1}p' infile > another.file
> 
> ```

thanks, that worked perfectly

---

<div class="post-metadata">

**Author:** ![babusek](https://community.unix.com/letter_avatar/babusek/32/6_449c533f586939b6e5f246632a46421f.png) [@babusek](https://community.unix.com/u/babusek)\
**Post date:** [January 5, 2010, 6:43am UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/5 "2010-01-05T06:43:10Z")

</div>

HI Can u please tell if i pass the ,

start and end patterns as arguments i mean

```nohighlight
nawk '/$end_Pattern/{print;exit} /$start_Pattern/{p=1}p' infile > another.file

```

where end\_Pattern=bye  
start\_Pattern=hello

but the above is not working good

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [January 5, 2010, 7:23am UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/6 "2010-01-05T07:23:28Z")

</div>

Hi.

You're trying to use shell variables inside awk, which doesn't work.

Either use something like:

```nohighlight
awk '/'$start_pattern'/,/'$end_pattern'/' infile

```

or (taking the steadyonabix example)

```nohighlight
awk -v S=$start_pattern -v E=$end_pattern '$0 ~ E {print;exit} $0 ~ S{p=1}p' infile > another.file

```

---

<div class="post-metadata">

**Author:** ![babusek](https://community.unix.com/letter_avatar/babusek/32/6_449c533f586939b6e5f246632a46421f.png) [@babusek](https://community.unix.com/u/babusek)\
**Post date:** [January 5, 2010, 7:29am UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/7 "2010-01-05T07:29:36Z")

</div>

Thaks @scott

The First Command giving the lines along with the start\_pattern and end\_pattern.:eek:  
can u please tell me the exact command to output the lines only between the pattern's.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [January 5, 2010, 7:34am UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/8 "2010-01-05T07:34:59Z")

</div>

You never said you didn't want the start and end patterns. And the original question asked for them. :rolleyes:

```nohighlight
nawk -v E=$E -v S=$S '$0 ~ E {exit} $0 ~ S {getline; p=1}p' infile

```

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_449c533f586939b6e5f246632a46421f.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [January 5, 2010, 6:13pm UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/9 "2010-01-05T18:13:06Z")

</div>

> [@steadyonabix](#):
>
> Don't do it youngster, learn a bit of awk, it will give your life new meaning 😉
> 
> ```plaintext
> nawk '/bye/{print;exit} /hello/{p=1}p' infile > another.file
> 
> ```

Good solution.

---

<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:** [January 5, 2010, 7:11pm UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/10 "2010-01-05T19:11:21Z")

</div>

```nohighlight
sed '0,/hello/d;/bye/,$d' infile > outfile

```

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_449c533f586939b6e5f246632a46421f.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [January 5, 2010, 10:18pm UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/11 "2010-01-05T22:18:34Z")

</div>

> [@scrutinizer](#):
>
> ```plaintext
> sed '0,/hello/d;/bye/,$d' infile > outfile
> 
> ```

For those not using a sed that supports the 0 address extension (as far as I know, that's a GNU extension), the following alternative will work:

```plaintext
sed -n '/hello/ {
          :next
          n; /bye/q; p; b next
        }' infile > outfile

```

Regards,  
alister

---

<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:** [January 6, 2010, 2:17am UTC](https://community.unix.com/t/how-can-i-get-the-lines-between-two-patterns/251225/12 "2010-01-06T02:17:03Z")

</div>

```nohighlight
awk '/bye/{p=0}p;/hello/{p=1}' infile

```
