Help with a multi-line sed substitution
Help with a multi-line sed substitution
am 18.10.2007 15:42:00 von whargrove
Hi there,
I'm having difficulty in trying to do a multi-line sed replacement on
the following text in an xml file.
I want to alter this to :
compilerSourceVM
1.5
i.e. to remove the bottom --> comment and close the comment on the
first line. Assuming the text is in a file like text.xml, I've tried
the following to no avail.
cat test.xml | sed -e '#^\n
\n compilerSourceVM\n
value>1.5\n \n#}'
I'm sure using the N command is the way to go but I don't seem to be
able to get this to work. Instead of re-writing the output I'd also
rather save and replay the lines within the comments.
I should add that there are other commented out lines in the file that
shouldn't be changed.
Any help would be gratefully received. Many thanks, Will.
Re: Help with a multi-line sed substitution
am 18.10.2007 16:36:25 von cichomitiko
whargrove wrote ...
[...]
>
>
> I want to alter this to :
>
>
>
> compilerSourceVM
> 1.5
>
>
> i.e. to remove the bottom --> comment and close the comment on the
> first line. Assuming the text is in a file like text.xml, I've tried
> the following to no avail.
[...]
> I should add that there are other commented out lines in the file that
> shouldn't be changed.
You asked for a sed solution, but in case awk is acceptable:
awk '/");f=1;print;next}
f&&/-->/{f=0;next}1' test.xml
Dimitre
P.S. Use nawk or /usr/xpg4/bin/awk on Solaris.
Re: Help with a multi-line sed substitution
am 18.10.2007 19:23:34 von Ed Morton
Radoulov, Dimitre wrote:
> whargrove wrote ...
> [...]
>
>>
>>
>>I want to alter this to :
>>
>>
>>
>> compilerSourceVM
>> 1.5
>>
>>
>>i.e. to remove the bottom --> comment and close the comment on the
>>first line. Assuming the text is in a file like text.xml, I've tried
>>the following to no avail.
>
> [...]
>
>>I should add that there are other commented out lines in the file that
>>shouldn't be changed.
>
>
> You asked for a sed solution, but in case awk is acceptable:
>
> awk '/");f=1;print;next}
> f&&/-->/{f=0;next}1' test.xml
>
It can be a bit simpler:
awk 'sub(/"){f=1}
f&&/-->/{f=0;next}1' test.xml
Regards,
Ed.
Re: Help with a multi-line sed substitution
am 18.10.2007 19:43:32 von cichomitiko
"Ed Morton" wrote ...
> Radoulov, Dimitre wrote:
>> whargrove wrote ...
>> [...]
>>
>>>
>>>
>>>I want to alter this to :
>>>
>>>
>>>
>>> compilerSourceVM
>>> 1.5
>>>
>>>
>>>i.e. to remove the bottom --> comment and close the comment on the
>>>first line. Assuming the text is in a file like text.xml, I've tried
>>>the following to no avail.
>>
>> [...]
>>
>>>I should add that there are other commented out lines in the file that
>>>shouldn't be changed.
>>
>>
>> You asked for a sed solution, but in case awk is acceptable:
>>
>> awk '/");f=1;print;next}
>> f&&/-->/{f=0;next}1' test.xml
>>
>
> It can be a bit simpler:
>
> awk 'sub(/"){f=1}
> f&&/-->/{f=0;next}1' test.xml
[...]
I tried with a test file like this:
zsh 4.3.4% cat test.xml
zsh 4.3.4% awk 'sub(/"){f=1}
quote> f&&/-->/{f=0;next}1' test.xml
compilerSourceVM
1.5
-->
Perhaps like this:
zsh 4.3.4% awk 'sub(/"){print;f=1;next}
f&&/-->/{f=0;next}1' test.xml
compilerSourceVM
1.5
Regards
Dimitre
Re: Help with a multi-line sed substitution
am 18.10.2007 21:45:28 von William James
On Oct 18, 12:43 pm, "Radoulov, Dimitre"
wrote:
> "Ed Morton" wrote ...> Radoulov, Dimitre wrote:
> >> whargrove wrote ...
> >> [...]
>
> >>>
>
> >>>I want to alter this to :
>
> >>>
> >>>
> >>> compilerSourceVM
> >>> 1.5
> >>>
>
> >>>i.e. to remove the bottom --> comment and close the comment on the
> >>>first line. Assuming the text is in a file like text.xml, I've tried
> >>>the following to no avail.
>
> >> [...]
>
> >>>I should add that there are other commented out lines in the file that
> >>>shouldn't be changed.
>
> >> You asked for a sed solution, but in case awk is acceptable:
>
> >> awk '/");f=1;print;next}
> >> f&&/-->/{f=0;next}1' test.xml
>
> > It can be a bit simpler:
>
> > awk 'sub(/"){f=1}
> > f&&/-->/{f=0;next}1' test.xml
>
> [...]
>
> I tried with a test file like this:
>
> zsh 4.3.4% cat test.xml
>
>
>
>
> zsh 4.3.4% awk 'sub(/"){f=1}
> quote> f&&/-->/{f=0;next}1' test.xml
>
>
> compilerSourceVM
> 1.5
>
> -->
>
>
> Perhaps like this:
>
> zsh 4.3.4% awk 'sub(/"){print;f=1;next}
> f&&/-->/{f=0;next}1' test.xml
>
>
>
> compilerSourceVM
> 1.5
>
>
sub(/"){f=9}
f && /^[ \t]*-->/ {f=0; next}
9
Re: Help with a multi-line sed substitution
am 18.10.2007 21:56:02 von cichomitiko
"William James" wrote ...
> On Oct 18, 12:43 pm, "Radoulov, Dimitre" > wrote:
>> "Ed Morton" wrote ...> Radoulov, Dimitre wrote:
>> >> whargrove wrote ...
>> >> [...]
>>
>> >>>
>>
>> >>>I want to alter this to :
>>
>> >>>
>> >>>
>> >>> compilerSourceVM
>> >>> 1.5
>> >>>
>> >> [...]
[...]
>> >> awk '/");f=1;print;next}
>> >> f&&/-->/{f=0;next}1' test.xml
>>
>> > It can be a bit simpler:
>>
>> > awk 'sub(/"){f=1}
>> > f&&/-->/{f=0;next}1' test.xml
>>
>> [...]
[...]
> sub(/"){f=9}
> f && /^[ \t]*-->/ {f=0; next}
> 9
[...]
Excellent!
Thanks
Dimitre
Re: Help with a multi-line sed substitution
am 18.10.2007 23:16:04 von Ed Morton
Radoulov, Dimitre wrote:
> "William James" wrote ...
>
>>On Oct 18, 12:43 pm, "Radoulov, Dimitre" > wrote:
>>
>>>"Ed Morton" wrote ...> Radoulov, Dimitre wrote:
>>>
>>>>>whargrove wrote ...
>>>>>[...]
>>>
>>>>>>
>>>
>>>>>>I want to alter this to :
>>>
>>>>>>
>>>>>>
>>>>>> compilerSourceVM
>>>>>> 1.5
>>>>>>
>>>>>
>>>>>[...]
>
> [...]
>
>>>>>awk '/");f=1;print;next}
>>>>>f&&/-->/{f=0;next}1' test.xml
>>>
>>>>It can be a bit simpler:
>>>
>>>>awk 'sub(/"){f=1}
>>>>f&&/-->/{f=0;next}1' test.xml
>>>
>>>[...]
>
> [...]
>
>>sub(/"){f=9}
>>f && /^[ \t]*-->/ {f=0; next}
>>9
>
> [...]
>
> Excellent!
Huh? You're claiming that on this input:
this:
sub(/"){f=9}
f && /^[ \t]*-->/ {f=0; next}
9
produces the desired output:
compilerSourceVM
1.5
but this:
sub(/"){f=1}
f && /-->/ {f=0; next}
1
produces this:
compilerSourceVM
1.5
-->
I don't have access to anything to test that on right now, but it seems
unlikely....
Ed.
Re: Help with a multi-line sed substitution
am 19.10.2007 10:24:41 von cichomitiko
Ed Morton wrote:
[...]
> Huh? You're claiming that on this input:
>
>
>
> this:
>
> sub(/"){f=9}
> f && /^[ \t]*-->/ {f=0; next}
> 9
>
> produces the desired output:
>
>
>
> compilerSourceVM
> 1.5
>
>
> but this:
>
> sub(/"){f=1}
> f && /-->/ {f=0; next}
> 1
>
> produces this:
>
>
> compilerSourceVM
> 1.5
>
> -->
>
> I don't have access to anything to test that on right now, but it seems
> unlikely....
[...]
Yes,
I suppose that's because of the pattern "-->" in the first record(after
the substitution):
$ cat file
$ awk --version|head -1
GNU Awk 3.1.5
$ awk 'sub(/"){f=1}
f && /-->/ {f=0; next}
1' file
compilerSourceVM
1.5
-->
$ awk 'sub(/"){f=1}
f && /^-->/ {f=0; next}
1' file
compilerSourceVM
1.5
$
Regards
Dimitre
Re: Help with a multi-line sed substitution
am 19.10.2007 15:01:47 von Ed Morton
Radoulov, Dimitre wrote:
> I suppose that's because of the pattern "-->" in the first record(after
> the substitution):
>
> $ cat file
>
> $ awk --version|head -1
> GNU Awk 3.1.5
> $ awk 'sub(/"){f=1}
> f && /-->/ {f=0; next}
> 1' file
>
> compilerSourceVM
> 1.5
>
> -->
Aaarggh. Of course you're right. So either of these will do it:
awk 'f && /-->/ {f=0; next}
sub(/"){f=1}
1' file
awk 'sub(/"){f=1}
f && /^[[:space:]]*-->/ {f=0; next}
1' file
Thanks for bearing with me!
Ed.