🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.editors
Article #4041

Re: [sed] Deleting Received: headers from file

#4041
From: Jim Monty
Date: Sun, 05 Aug 2001 06:28
65 lines
2567 bytes
Charles Calvert <cbciv@yahoo.com> wrote:
> I'm using GNU sed 3.02 recompiled for Win32.
>
> I'd like to write a sed script that removes all Received: headers from
> a mail message in a file.  It should work with headers that span 0..N
> lines.  I spent some time searching the web and DejaGoogle today and
> didn't find anything that does what I want or imparted enough of a
> clue for me to figure it out.
>
> I've just started using sed, so if my current attempt looks idiotic,
> that's why.
>
> Here's the script I've written.
>
> /^Received:.*/{
> :START
> d
> n
> /^[[:space:]].*/{b START}
> }
>
> I think that it should do the following:
>
> if matches expression "^Received:.*"
> 	delete the current line
> 	read in the next line
> 	if matches expression "^[[:space:]].*"
> 		go to START
> 	else
> 		output current line
>
> The actual result is that it strips the first line of a multiline
> Received header, but leaves any subsequent lines belonging to that
> header.  I've probably been staring at it too long, but I can't see
> what I'm doing wrongly.

It's easier and more sensible in awk:

FNR == 1 { in_headers = 1 }

in_headers {
    if (/^Received:/) {
        in_received_header = 1
        next       # skip first line of Received: headers
    } else if (in_received_header) {
        if (/^[[:space:]]/) {
            next   # skip second and subsequent lines of Received: headers
        } else {
            in_received_header = 0
        }
    }

    if (! length) {
        in_headers = 0
    }
}

{ print }

Perl has modules for parsing and editing Internet mail.

-- 
Jim Monty
monty@primenet.com
Tempe, Arizona USA
Technical Details
Message-ID: <9kip2p$ltj$2@nnrp1.phx.gblx.net>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!arclight.uoregon.edu!csulb.edu!tethys.csu.net!nntp.primenet.com!nntp.gblx.net!news.phx.gblx.net!monty
References: <0qemmtcr1t6u6lfmqvt87kvn10jgk51u5p@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109f73,f5da5342e4a193e2
X-Google-Attributes: gid109f73,public
X-Google-Thread: 107393,f5da5342e4a193e2
X-Google-Attributes: gid107393,public
X-Google-ArrivalTime: 2001-08-04 23:28:45 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!arclight.uoregon.edu!csulb.edu!tethys.csu.net!nntp.primenet.com!nntp.gblx.net!news.phx.gblx.net!monty
From: Jim Monty <monty@primenet.com>
Newsgroups: comp.editors,gnu.utils.help
Subject: Re: [sed] Deleting Received: headers from file
Date: 5 Aug 2001 06:28:41 GMT
Organization: Primenet
Lines: 65
Message-ID: <9kip2p$ltj$2@nnrp1.phx.gblx.net>
References: <0qemmtcr1t6u6lfmqvt87kvn10jgk51u5p@4ax.com>
NNTP-Posting-Host: usr01.primenet.com
X-Complaints-To: abuse@gblx.net
X-Posted-By: monty@206.165.6.201 (monty)