[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
sed, line range, inclusive/exclusive
From: |
Jim Easton |
Subject: |
sed, line range, inclusive/exclusive |
Date: |
Tue, 6 Apr 2004 01:03:54 -0600 (MDT) |
Dear People,
I don't think I could properly call this a bug - more like a
simple question. I have been using sed for 20 years and have
never figured out how to do the following:
How does one select a group of lines the first of which has a
unique pattern and the following lines another pattern.
Or to rephrash this the info/man pages say that one can select
a range of lines with two addresses the second of which is
*inclusive*. How can I make the second address *exclusive*?
ie. *not* included in the range.
This came up for the umpteenth time when I was trying to sift
email messages which contain, among a lot of other stuff, several
sets of lines begining with "Received:" and followed by lines
begining with a "tab" I would like to delete (or select them
for some sort of substitution) that group of lines without
necessarily deleting the following non-tab line. It might or
might not be be another "Received:" line which one would like
to select or not.
I have tried many things none of which worked. Including the
"c" command, which incidently didn't allow for a null text.
Following is a some test data, just one of many desperation
scripts and the output.
Please tell me how an expert would accomplish this. I could do
it in shell, C or my knowledge of perl (God forbid) is sketchy
at best but I wanted to use sed.
Jim
Following is a simplified test message:
>From address@hidden Mon Mar 8 21:28:35 2004
Received: line 0 0
line 0 1
line 0 2
line 0 2
Received: line 1 0
line 1 1
line 1 2
line 1 3
Received: line 2 0
line 2 1
line 2 2
line 2 3
line 2 4
Received: line 3 0
line 3 1
line 3 2
line 3 3
line 3 4
Date: Mon, 08 Mar 2004 20:22:05 -0800
From: address@hidden
Subject: this is a test
This is the body of the message
#!/bin/sh
# attempt to delete "Received:" groups.
cat m1 |
sed '
/^Received:[ ]/,/^[^ ]/ {
s/^\(Received:\)/\1 DALETE/
s/^ / DELLTE /
}
/^Received:[ ]/,/^[^ ]/ {
s/^\(Received:\)/\1 DALL /
s/^ / DELL/
}
'
Produced the following output from the above test data.
>From address@hidden Mon Mar 8 21:28:35 2004
Received: DALL DALETE line 0 0
DELL DELLTE line 0 1
DELL DELLTE line 0 2
DELL DELLTE line 0 2
Received: DALL DALETE line 1 0
line 1 1
line 1 2
line 1 3
Received: DALL DALETE line 2 0
DELL DELLTE line 2 1
DELL DELLTE line 2 2
DELL DELLTE line 2 3
DELL DELLTE line 2 4
Received: DALL DALETE line 3 0
line 3 1
line 3 2
line 3 3
line 3 4
Date: Mon, 08 Mar 2004 20:22:05 -0800
From: address@hidden
Subject: this is a test
This is the body of the message
- sed, line range, inclusive/exclusive,
Jim Easton <=