awk - Print chunk of lines between matching patterns -


i have bunch of email archives looks pattern below. trying separate emails separated starting from: , ending pattern from: .

from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: greetings date: 04/05/1990 10:30 pm  hello. world test email.   from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: new greetings date: 04/05/1990 10:30 pm  yet test email 

using awk, awk '/from/{p=1} p; /date/{exit}' text.txt. gives:

from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: greetings date: 04/05/1990 10:30 pm 

how can possibly modify awk command get:

from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: greetings date: 04/05/1990 10:30 pm  hello. world test email. 

thanks in advance

if not bothered newline char

$ awk '/^from:/{count++} count==1' infile   $ awk -v mail_no=1 '/^from:/{count++}count==mail_no; count>mail_no{exit}' infile  

examples :

$ awk '/^from:/{count++} count==1' infile  from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: greetings date: 04/05/1990 10:30 pm  hello. world test email.   $ awk '/^from:/{count++} count==2' infile  from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: new greetings date: 04/05/1990 10:30 pm  yet test email 

to remove trailing newline char, prefer use tac below

$ awk -v mail_no=1 '/^from:/{count++}count==mail_no;count>mail_no{exit} ' infile | tac | awk 'nf{found=1}found' | tac from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: greetings date: 04/05/1990 10:30 pm  hello. world test email. 

input :

$ cat infile from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: greetings date: 04/05/1990 10:30 pm  hello. world test email.   from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: new greetings date: 04/05/1990 10:30 pm  yet test email   from: bikram suwal veekram@gmail.com to: john doe johndoe@gmail.com subject: new greetings date: 04/05/1990 10:30 pm  yet test email 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -