Description
  1. A mailbox contains one more more messages.
  2. Each message contains one or more 'Received:' headers.
  3. A 'Received:' header consists of one or more lines. The second and following ones are indented by spaces or tabs.
  4. In a message, no 'Received:' header is separated from other 'Received:' headers by non-'Received:' headers.
Raw Input
From sed-users@yahoogroups.com  Sun May  9 14:52:11 2004
Return-Path: <sentto-changyj=rtfiber.com.tw@returns.groups.yahoo.com>
Received: from n11.grp.scd.yahoo.com (n11.grp.scd.yahoo.com [66.218.66.66])
	by main.rtfiber.com.tw (8.11.6/8.11.6) with SMTP id i170Lq809415
	for <changyj@rtfiber.com.tw>; Sat, 7 Feb 2004 08:21:52 +0800
Received: (qmail 74534 invoked from network); 7 Feb 2004 00:21:52 -0000
Received: from unknown (HELO n17.grp.scd.yahoo.com) (66.218.66.72)
  by mta2.grp.scd.yahoo.com with SMTP; 7 Feb 2004 00:21:51 -0000
To: sed-users@yahoogroups.com
From: "john_vdv" <jvdv@spam.net>

Welcome to the world of Regular Expressions!
From tester@cracker.org Sun May 9 14:52:11 2004 Return-Path: <tester@cracker.org> Received: from solomon.hq (solomon.hq [127.0.0.1]) by solomon.hq (Postfix) with SMTP id 355263025A for <changyj@localhost.localdomain> Date: Sun, 9 May 2004 14:51:57 +0800 (CST) From: tester@cracker.org To: undisclosed-recipients:;
Regular expressions are powerful!
Desired Output
Received: from n11.grp.scd.yahoo.com (n11.grp.scd.yahoo.com [66.218.66.66])
	by main.rtfiber.com.tw (8.11.6/8.11.6) with SMTP id i170Lq809415
	for <changyj@rtfiber.com.tw>; Sat, 7 Feb 2004 08:21:52 +0800
Received: (qmail 74534 invoked from network); 7 Feb 2004 00:21:52 -0000
Received: from unknown (HELO n17.grp.scd.yahoo.com) (66.218.66.72)
  by mta2.grp.scd.yahoo.com with SMTP; 7 Feb 2004 00:21:51 -0000

Received: from solomon.hq (solomon.hq [127.0.0.1]) by solomon.hq (Postfix) with SMTP id 355263025A for <changyj@localhost.localdomain>
Script and Comments
Script1
[ 1] /^Received:/!d
[ 2] :loop
[ 3] N
[ 4] /\nReceived:[^\n]*$/b loop
[ 5] /\n[\t ][^\n]*$/b loop
[ 6] s/\n[^\n]*$/\n/
Comments
  1. Each messages will contain exactly one 'Received:' block which consists of one or more 'Received:' headers.
  2. Step [2] thru step [5] constitute a loop, which will join all 'Received:' headers of a 'Received:' block.
  3. If the line joined to Pattern Space by Step [3]:
    • begins with 'Received:', it is the first line of a 'Received:' header, Step [4] will make sed branch to Step [2].
    • begins with spaces or TABs, it is part of a 'Received:' header, Step [5] will make sed branch to Step [2]. In GNU sed, you can use '\t' to match a TAB.
    • Otherwise, it is NOT part of the 'Received:' block in question, and all lines of Pattern Space except it constitute a 'Received:' block.
  4. Step [6] is used to print a 'Received:' block and one blank line.