| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
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.
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
$ sed '/<property name="name">/{:loop /<\/property>.*<\/property>/d; N;
b loop}' file.xml
Stuff that comes before
More stuff that comes before
Stuff that comes after
More stuff that comes after
Stuff that comes after
More stuff that comes after
BTW, in the orig post, I think you meant to say #1, not #2?
#1 <property name="name">.*$
#2 <property name="name">*$
Daniel
On 10/5/2014 3:12 PM, 'Marcus L. Thompson' [email protected] [sed-users]
wrote:
> Thank you, Ronaldo, for your help in this small matter.
>
> I've been struggling with just how to expand my usage of sed beyond the
> rudimentary s/a/b/ type implementation; and this was a great excuse for
> a question ;)
>
> Syntax formation seems to be the biggest obstacle for me with regard to
> sed. Thank you again for the working snippet to work through & learn
> from...
>
> Cheers!
>
>
> -Marcus
>
>
> On 10/04/2014 06:13 PM, Ronaldo Ferreira de Lima [email protected]
> [sed-users] wrote:
>>
>> Greetings Marcus,
>>
>> On Fri, Oct 03, 2014 at 08:44:36PM -0400, 'Marcus L. Thompson'
>> [email protected] wrote:
>>> Greetings to all.
>>>
>>> I have come across what seems to be a quite simple problem involving an
>>> application of sed in the context of text-block deletion. Here's the
>>> hypothetical text block in question:
>>>
>>> <property name="name">
>>> ...
>>> </property>
>>> </property>
>>>
>>> Now, deleting all from <property name="name"> to the _/first/_
>>> </property> tag is understandable enough:
>>>
>>> Code:
>>>
>>> sed -i '/<property name="name">*$/,/<\/property>*$/d' /file.xml
>>>
>>> However, incorporating the 2nd </property> tag in the deletion sweep is
>>> a bit tougher; and just that much outside my new skillset range ;o)
>>>
>>> Any ideas as to how one might amend this "one-liner" to make it all
>> work?
>> You might try:
>>
>> $ sed '/<property
>> name="name">/{:loop;/<\/property>.*<\/property>/{s/.*//;b;};N;b
>> loop;};' file.xml
>>
>> But this is not a good approach to edit XML files. Have a look in XML
>> parsers like xmllint or xsh (command-line applications).
>>
>>
>
>
>
>
> ----
>
> [Non-text portions of this message have been removed]
>
>
>
> ------------------------------------
> Posted by: "Marcus L. Thompson" <[email protected]>
> ------------------------------------
>