Re: A Simple Text-Region Deletion Question . . .

"Ronaldo Ferreira de Lima [email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <20141006225339.GB1102@textmode>
Greetings Daniel,

On Sun, Oct 05, 2014 at 11:24:19PM -0700, Daniel Goldman [email protected] wrote:
> I don't know why one would want to delete that kind of block, since it's
> not symmetrical. But you must have your reasons, it's better to just
> answer the question, as Ronaldo did. And as you say, it led to a great
> example to work through. sed can do an incredible amount in one line.
> I'd be interested in how Ronaldo came up with the solution, kind of his
> thinking process, if he cares to explain.
(Maybe I can't explain very well in English yet...)

I start thinking about how to put entire block in pattern space to
simplify tests and other manipulations:

/<property name="name">/ {         # search for regexp and start a block
    :loop;
    /<\/property>.*<\/property>/ { # when pattern space had two end-tags
        s/.*//;                    # substitute for nothing (I made a
                                   # mistake here, but your code fix
                                   # this)
        b;                         # avoid the next loop (not really
                                   # needed in this case)
     };
     N;                            # append next line
     b loop;                       # jump to label ":loop"
};


> FWIW, here is a variation of Ronaldo's solution that is perhaps a little
> better and simpler:
>
> $ cat file.xml
> Stuff that comes before
> More stuff that comes before
> <property name="name">
> ...
> </property>
> </property>
> Stuff that comes after
> More stuff that comes after
> <property name="name">
> ...
> </property>
> </property>
> Stuff that comes after
> More stuff that comes after
Good sample.

> $ sed '/<property name="name">/{:loop /<\/property>.*<\/property>/d; N;
> b loop}' file.xml
[...]
A hypothetical problem with my solution and your solution is in this case:

    Stuff that comes before
    More stuff that comes before
    <property name="name">
    ...
    </property>
    1
    2
    <property name="name">
    </property>
    Stuff that comes after
    More stuff that comes after
    <property name="name">
    ...
    </property>
    </property>
    Stuff that comes after
    More stuff that comes after

Our codes removes the "symmetric" block between "asymmetric" blocks. But
this  type of  things need  attention from  Marcus. Maybe  this isn't  a
problem.

A hypothetical problem with the Marcus solution is in this case:

    Stuff that comes before
    More stuff that comes before
    <property name="name">
    ...
    Stuff that comes after
    More stuff that comes after

Only the  first two lines  of this example  will be printed  because sed
delete  the  address  regexp  '/<property  name="name">*$/'  until  find
'/<\/property>*$/' OR find end of  file. Perhaps this is an undesirable
behavior.

[]'s
--
"Não manejo bem as palavras
Mas manipulo bem as strings."
------------------------------
http://tecnoveneno.blogspot.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.