title: How to extract email fields from headers with awk or grep
body: <p>Regarding: mailbox (mbox format) email messages</p>
  
  <p><strong>Multi Message File:</strong> Inbox.mbox  </p>
  
  <pre><code>From - Thu Mar 26 16:16:21 2015
  From: Mail Delivery System &lt;Mailer-Daemon@200.netwizz.com&gt;
  To: edge@notterribe.org
  Subject: Mail delivery failed: returning message to sender
  Message-Id: &lt;E1Yb3yX-0004CB-QH@200.netwizz.com&gt;
  Date: Thu, 26 Mar 2015 02:21:17 -0700
  Date: Thu, 26 Mar 2015 02:20:44 -0700
  From: edge &lt;edge@notterribe.org&gt;
  User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.5.0
  MIME-Version: 1.0
  To: leasing@theedgehenderson.com
  CC: etpmgr@movein.net, t.simmonds@movein.ne
  Subject: Fwd: Today's Breach Of Our Security.
  From - Fri Mar 27 12:00:00 2015  
  </code></pre>
  
  <p><strong>Desired Pattern Match Order;</strong>  </p>
  
  <pre><code>Date: Thu, 26 Mar 2015 02:21:17 -0700  
  From - Thu Mar 26 16:16:21 2015  
  From: Mail Delivery System &lt;Mailer-Daemon@200.netwizz.com&gt;  
  To: edge@notterribe.org  
  Message-Id: &amp;lt;E1Yb3yX-0004CB-QH@200.netwizz.com&gt;  
  Subject: Mail delivery failed: returning message to sender 
  </code></pre>
  
  <p><strong>Desired Final Result;</strong>  </p>
  
  <pre><code>Date: Thu; 26 Mar 2015 02:21:17 -0700;From - Thu Mar 26 16:16:21 2015;From: Mail Delivery System &lt;Mailer-Daemon@200.netwizz.com&gt;;To: edge@notterribe.org;Message-Id: &amp;lt;E1Yb3yX-0004CB-QH@200.netwizz.com&gt;;Subject: Mail delivery failed: returning message to sender
  </code></pre>
  
  <p><strong>Objectives;</strong><br>
  * Each mail message in "Inbox.mbox" starts with "From "<br>
  * Match 1st occurance once only for "^Date: |^From |^From: |^To: |^Message-Id: |^Subject: ", print that line.<br>
  * Format Output results in csv seperated by semicolon</p>
  
  <p>I've tried;<br>
  <code>grep -a -E -i "^Date: |^From |^From: |^To: |^Message-ID: |^Subject: " Inbox.mbox<br>
  awk '/^Date: / || /^From / || /^From: / || /^To: / || /^Message-ID: / || /^Subject: /' Inbox.mbox</code></p>
  
  <p><strong>Comments:</strong> The above gave me a good start, I'm most familiar with awk and grep so I'm trying to use them only. Having difficulty printing out the lines in order I wish, matching the 1st occurance only ending in a newline. Binary data existed in some messages so I used -a with grep.</p>
  
  <p>Any assistance would be most appreciated.<br>
  Thank you.</p>