# Retrieve message from file

**URL:** <https://community.unix.com/t/retrieve-message-from-file/168784>\
**Category:** Shell Programming and Scripting\
**Created:** [February 2, 2007, 6:50am UTC](https://community.unix.com/t/retrieve-message-from-file/168784 "2007-02-02T06:50:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpang](https://community.unix.com/letter_avatar/mpang/32/5_5575768a8748004e209b776fc1b2916d.png) [@mpang](https://community.unix.com/u/mpang)\
**Post date:** [February 2, 2007, 6:50am UTC](https://community.unix.com/t/retrieve-message-from-file/168784/1 "2007-02-02T06:50:00Z")

</div>

Hey all, I have this file which contains a lot of messages in format like

[2/2/07 13:23:00] This is my message!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Check This!  
[2/2/07 13:23:10] This is my second message!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
!!!!!!!!!!!!!!!!!!!!!!!!!  
[2/2/07 13:24:48] This is my third message!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
!!!!!!!!!!!!!!!!!!!!!!!!!

when I do "grep" on this file using "2/2/07 13:23", it returned

[2/2/07 13:23:00] This is my message!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
[2/2/07 13:23:10] This is my second message!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

but in fact I want

[2/2/07 13:23:00] This is my message!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Check This!  
[2/2/07 13:23:10] This is my second message!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
!!!!!!!!!!!!!!!!!!!!!!!!!

since these messages span over several lines, could anyone tell me how to achieve what I need? Thank you so much!

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [February 2, 2007, 7:13am UTC](https://community.unix.com/t/retrieve-message-from-file/168784/2 "2007-02-02T07:13:00Z")

</div>

I assume that your message start with [ and span over multiple lines

```nohighlight
awk ' /2\/2\/07 13:23/ { print ; getline ;while( match( $0 , "^[^\[]" ) || match( $0 ,"2\/2\/07 13:23") ) { print ; getline; } } ' file

```

---

<div class="post-metadata">

**Author:** ![Krrishv](https://community.unix.com/user_avatar/community.unix.com/krrishv/32/3542_2.png) [@Krrishv](https://community.unix.com/u/Krrishv)\
**Post date:** [February 2, 2007, 7:39am UTC](https://community.unix.com/t/retrieve-message-from-file/168784/3 "2007-02-02T07:39:51Z")

</div>

Anbu,

Can we do this using FS and RS in awk.

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [February 2, 2007, 8:01am UTC](https://community.unix.com/t/retrieve-message-from-file/168784/4 "2007-02-02T08:01:04Z")

</div>

> [@krrishv](#):
>
> Anbu,
> 
> Can we do this using FS and RS in awk.

This will work if [ is not anywhere other than the start of message

```plaintext
awk -v RS="[" ' /2\/2\/07 13:23/ { print "["$0 } ' file

```

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [February 2, 2007, 8:10am UTC](https://community.unix.com/t/retrieve-message-from-file/168784/5 "2007-02-02T08:10:27Z")

</div>

And add this to adjust the ORS:

```nohighlight
awk -v RS="[" -v ORS= '/2\/2\/07 13:23/{print RS$0}' infile

```

Or with sed:

```nohighlight
sed -n '/\[2\/2\/07 13:23/,/\[2\/2\/07 13:23/{N;p;}' infile

```

---

<div class="post-metadata">

**Author:** ![Krrishv](https://community.unix.com/user_avatar/community.unix.com/krrishv/32/3542_2.png) [@Krrishv](https://community.unix.com/u/Krrishv)\
**Post date:** [February 2, 2007, 8:38am UTC](https://community.unix.com/t/retrieve-message-from-file/168784/6 "2007-02-02T08:38:45Z")

</div>

Thanks & great radoulov..it was perfect.
