How to grab a block of data in a file with repeating pattern?

I need to send email to receipient in each block of data in a file which has the sender address under TO and just send that block of data where it ends as COMPANY.

I tried to work this out by getting line numbers of the string HELLO but unable to grab the next block of data to send the next email. i used commands like

grep -n "TO :" Sample2.txt|cut -d ":" -f 1,3| tr -d ' '

to get line numbers with email ids.

sed -n '/HELO/,/HELO/p' Sample2.txt this is where i am unable to get the corresponding block for that email id. :confused:

The sample file is

HELLO 

MAIL FROM: <A@B.COM>                                                                                                           

RCPT TO: <RCPT1@TEMP.COM>                                                                                                      

Date: 16 JUN 11 18:28:41 EST                                                                                                         

FROM: TEMPSENDER@EMAIL.COM                                                                                                                  

TO : TEMPRECEIVER@EMAIL.COM                                                                                                     

DATA 

               ABCDCDCDCDC
               
THANK YOU,                                                                                                                           

COMPANY

.

HELLO 

MAIL FROM: <A@B.COM>                                                                                                           

RCPT TO: <RCPT2@TEMP.COM>                                                                                                      

Date: 16 JUN 11 18:28:41 EST                                                                                                         

FROM: TEMPSENDER@EMAIL.COM                                                                                                                  

TO : TEMPRECEIVER2@EMAIL.COM                                                                                                     

DATA 

               ASDASDSADASDASD
               
THANK YOU,                                                                                                                           

COMPANY

In sed, you can read in each message up to COMPANY using 'N' and process it like a long line.

Hi,

How would you do that ?

You use the N to append the next line and loop back if COMPANY is not there:

sed '
  :lopp
  /COMPANY/!{
    N
    b loop
    }
  .
  .
  .
 '

The resulting buffer matches '^line1\nline2\n...lineN$'.

1 Like

Thanks, very useful.

PS: Looks like it ends in ^\.$ like smtp/esmtp data, not COMPANY.