From: Rakesh Sharma on
On Apr 18, 7:47 pm, moonhkt <moon...(a)gmail.com> wrote:
> On 4月18日, 上午1時22分, Rakesh Sharma <sharma...(a)hotmail.com> wrote:
>
>
>
> > On Apr 17, 7:36 pm, moonhkt <moon...(a)gmail.com> wrote:
>
> > > On 4月17日, 下午9時29分, Ed Morton <mortons...(a)gmail.com> wrote:
>
> > > > On 4/17/2010 8:17 AM, Rakesh Sharma wrote:
>
> > > > > On Apr 17, 8:25 am, moonhkt<moon...(a)gmail.com>  wrote:
> > > > >> On 4月17日, 上午12時23分, Ed Morton<mortons...(a)gmail.com>  wrote:
>
> > > > >>> On 4/16/2010 11:13 AM, moonhkt wrote:
>
> > > > >>>> Hi all
>
> > > > >>>> When add for replace next is blank line. Not work. How to fix this
> > > > >>>> problem ?
> > > > >>>>      /^$/ {
> > > > >>>>             N
> > > > >>>>             /^\n$/D
> > > > >>>>      }
>
> > > > >>> sed is an excellent tool for simple substitutions on a single line. For anything
> > > > >>> else you should use awk, perl, etc.
>
> > > > >>> Now, tell us what you're trying to do, with a small sample input and the
> > > > >>> expected output from that input (not the output some script that didn't work
> > > > >>> gives you as that's not useful), and one of us will show you a very clear,
> > > > >>> simple awk script that does it.
>
> > > > >>>       Ed.
>
> > > > >> Does sed can substitution/delete as below ? I am read a book, "sed&
> > > > >> awk" , O'Reilly also Perl.
>
> > > > >> When using two sed, the result is ok
>
> > > > >> sed '/^Delete Database/d
> > > > >>       /^end of message/d
> > > > >>          ' sed.text | sed '/^$/{
> > > > >>     N
> > > > >>     /^\n$/D
> > > > >>          }
> > > > >>     '
>
> > > > >> Input file
> > > > >> ======
> > > > >> $ cat sed.text
> > > > >> /* testing_email_body.txt */
> > > > >> UNIX SILENT cat /phx/src/testing_email_body.txt \>
>
> > > > >> end of message
>
> > > > >> Delete Database
>
> > > > >> line 9
> > > > >> Output file
> > > > >> =======
> > > > >> /* testing_email_body.txt */
> > > > >> UNIX SILENT cat /phx/src/testing_email_body.txt \>
>
> > > > >> line 9
>
> > > > > you need to be clear about what each command of "sed" does.
>
> > > > > sed -e '
> > > > >     /^Delete Database/d
>
> > > > >     /^end of message/d
>
> > > > >     /^$/ {
> > > > >        $q;N
> > > > >        /^\n./P
> > > > >        D
> > > > >     }
> > > > > ' yourfile
>
> > > > Rakesh - can you tells us what that does? The OP seems to not understand what
> > > > I'm asking and I can't figure out what he's trying to do by reading the scripts.
>
> > > >      Ed.
>
> > > I still want using one sed to output the result. Using two sed for
> > > mark sure that sed can remove second blank line.
>
> > > I try below code.
> > > sed -e '
> > >    /^Delete Database/d
> > >    /^end of message/d
> > >    /^$/ {
> > >       N
> > >       /^\n$/P
> > >           D
> > >    }
>
> > > The output as below, but missing one blank line between line 9 and
> > > line 11.
> > > /* testing_email_body.txt */
> > > UNIX SILENT cat /phx/src/testing_email_body.txt \>
> > > line 3
>
> > > line 9
> > > line 11
>
> > > Input
> > > $ cat sed.text
> > > /* testing_email_body.txt */
> > > UNIX SILENT cat /phx/src/testing_email_body.txt \>
> > > line 3
> > > end of message
>
> > > Delete Database XXXXXXXXXX
>
> > > line 9
>
> > > line 11
>
> > > Expect Output
> > > $ cat sed.text
> > > /* testing_email_body.txt */
> > > UNIX SILENT cat /phx/src/testing_email_body.txt \>
> > > line 3
>
> > > line 9
>
> > > line 11
>
> > Please copy/paste whatever was provided to you. You are using /^\n$/P
> > in place of /^\n./P
> > They are actually reverse of each other!
>
> > sed -e '
> >    /^Delete Database/d
>
> >    /^end of message/d
>
> >    /^$/ {
> >       $q;N
> >       /^\n./P; #  you used a DOLLAR in place of a DOT
> >       D
> >    }
> > '  yourfile
>
> > Or you could use this:
>
> > sed -e '
> >    /^Delete Database/d
>
> >    /^end of message/d
>
> >    /./b
>
> >    $q;N
> >    /../P;D
> > ' yourfile
>
> > Rough explanation goes somewhat like this:
> > Till you meet an empty line (empty line == no characters, not even
> > spaces, or tabs) do the following:
> >     i) delete line beginning with "Delete Database".
> >    ii) delete line beginning with "end of message".
> >   iii) any other nonempty line, just print it.
>
> > when we hit the first empty line, we read in the next line also to do
> > something. only 2 cases can
> > occur
>
> > EE: empty -> empty.         This can be matched by: /^\n$/ Just one
> > character for two empty lines & that's a newline
> > EN: empty -> nonempty.    This can be matched by: /^\n./ or /.../
> > Atleast two characters for empty followed by nonempty.
>
> > For EE we Delete the first empty, & without reading in the next input
> > line, go back to top of script with whatever is remaining in
> > our pattern space (this also is an empty line, remember). so we again
> > hit the /^\n$/{....} code of script & end up reading the next line.
> > This has the effect of compressing multiple empty lines into a single
> > empty line.
>
> > For EN, we print the empty line (/^\n./P) then Delete it from the
> > pattern space. Now the pattern space is holding the nonempty line. Due
> > to the nature
> > of the D command we reapply the script to whatever remained in the
> > pattern space after we Deleted.
>
> > This could be very easily captured if you drew a flowchart for this.
>
> > -- Rakesh
>
> Thank for your suggestion. I need something to understanding/testing.
>
> But, I test below coding.
> sed -e '
>    /^Delete Database/d
>
>    /^end of message/d
>
>    /^$/ {
>       $q;N
>       /^\n./P; #  you used a DOLLAR in place of a DOT
>       D
>    }
> '  sed.text
>
> Input file
> /* testing_email_body.txt */
> UNIX SILENT cat /phx/src/testing_email_body.txt \>
> line 3
> end of message
>
> Delete Database XXXXXXXXXX
>
> line 10
>
> line 19
>
> Output file, two blank lines between line 3 and line 10. Suppose, One
> blank line.
>
> $ sed_del.ksh
> /* testing_email_body.txt */
> UNIX SILENT cat /phx/src/testing_email_body.txt \>
> line 3
>
> line 10
>
> line 19
>
> When  Add "line 6" in line 6, The Result is OK.
>
> Input file
> /* testing_email_body.txt */
> UNIX SILENT cat /phx/src/testing_email_body.txt \>
> line 3
> end of message
>
> line 6
> Delete Database XXXXXXXXXX
>
> line 10
>
> line 19
>
> $ sed_del.ksh
> /* testing_email_body.txt */
> UNIX SILENT cat /phx/src/testing_email_body.txt \>
> line 3
>
> line 6
>
> line 10
>
> line 19



You are changing the specs midway, that's why you saw those extra
empty lines.
Until now we just casually processed the combo: EN
Turns out, we need to make it more granular.


sed -e '
/^Delete Database/d

/^end of message/d

/^$/{
$q;N
;# case-1: E, E
/^\n$/D

;# case-2: E,Delete Database
/^\nDelete Database/{
s/^\(\n\).*/\1\1/;D
}

;# case-3: E, end of message
/^\nend of message/{
s/^\(\n\).*/\1\1/;D
}

;# case-4: E, any other type of nonempty
P;D
}
' yourfile

-- Rakesh