GNU bug report logs - #28056
Substitution doing global when it should not.

Previous Next

Package: sed;

Reported by: Techwolf Lupindo <techwolf.lupindo <at> gmail.com>

Date: Fri, 11 Aug 2017 20:15:01 UTC

Severity: normal

Tags: notabug

Done: Eric Blake <eblake <at> redhat.com>

Bug is archived. No further changes may be made.

Full log


Message #18 received at 28056-done <at> debbugs.gnu.org (full text, mbox):

From: Techwolf Lupindo <techwolf.lupindo <at> gmail.com>
To: Eric Blake <eblake <at> redhat.com>
Cc: 28056-done <at> debbugs.gnu.org
Subject: Re: bug#28056: Substitution doing global when it should not.
Date: Sat, 12 Aug 2017 14:02:38 -0400
[Message part 1 (text/plain, inline)]
A followup. I tried the command you hinted at. The result was the first
match and substitution was done, but the rest of the file was deleted. This
is using sed -i -e.

On Fri, Aug 11, 2017 at 10:19 PM, Techwolf Lupindo <
techwolf.lupindo <at> gmail.com> wrote:

> The docs say:
> g
> Apply the replacement to all matches to the regexp, not just the first.
>
> Nothing in the docs say it will matches all lines in the input, wither
> piped or file inplace. Doc bug?
>
> On Fri, Aug 11, 2017 at 6:39 PM, Eric Blake <eblake <at> redhat.com> wrote:
>
>> tag 28056 notabug
>> thanks
>>
>> On 08/11/2017 03:04 PM, Techwolf Lupindo wrote:
>>
>> > techwolf <at> laptop ~/test $ sed -e 's:COMMAND ${MERCURIAL}:COMMAND
>> > ${MERCURIAL} --cwd ${CMAKE_SOURCE_DIR}:' BuildVersion.cmake >
>> > BuildVersion1.cmake
>> > techwolf <at> laptop ~/test $ sed -e 's:COMMAND ${MERCURIAL}:COMMAND
>> > ${MERCURIAL} --cwd ${CMAKE_SOURCE_DIR}:g' BuildVersion.cmake >
>> > BuildVersion2.cmake
>>
>>
>> >
>> > The only difference in the sed commands was s/// and s///g.
>>
>> Which tells sed whether to make one substitution on every affected line,
>> or as many substitutions as possible on every affected line.  But the
>> number of affected lines remains unchanged for the two variants.
>>
>> > The diff
>> > between BuildVersion1.cmake and BuildVersion2.cmake should had one
>> change.
>>
>> Rather, one change per affected line.
>>
>> > But sed is matching two lines without the s///g. All docs I have read
>> says
>> > that without 'g', only the first match is changed.
>>
>> Only the first match per line - but when both lines affected only had
>> one match per line, there's no difference in using the 'g' flag.
>>
>> If you want sed to stop processing as soon as it has changed one line,
>> rather than going on to look for additional affected lines, you can do so:
>>
>> sed -e '/COMMAND ${MERCURIAL}/ { s::COMMAND ${MERCURIAL} -- cwd
>> ${CMAKE_SOURCE_DIR}: ; q }'
>>
>> which says find the first line that matches the pattern, and on that
>> line make the substitution and immediately quit.
>>
>> Since sed is behaving as documented, I'm closing this as not a bug;
>> however, feel free to add further comments to this thread if you need
>> followup clarification.
>>
>> --
>> Eric Blake, Principal Software Engineer
>> Red Hat, Inc.           +1-919-301-3266
>> Virtualization:  qemu.org | libvirt.org
>>
>>
>
[Message part 2 (text/html, inline)]

This bug report was last modified 7 years and 280 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.