# Help a sed noob

11 messages from 2007-08-03 to 2007-08-04. Participants: Nguyen Thai Ngoc Duy, Junio C Hamano, René Scharfe, martin f krafft, Jeff King, Simon 'corecode' Schubert, David Kastrup.
Thread: https://gitlist.dev/t/9372

## Nguyen Thai Ngoc Duy, 2007-08-03 23:22

Subject: Help a sed noob
Message-ID: <fcaeb9bf0708031622w25ab6e9ev61372169cfc0bd98@mail.gmail.com>
URL: https://gitlist.dev/e/fcaeb9bf0708031622w25ab6e9ev61372169cfc0bd98%40mail.gmail.com

```
Hi,

There is a construct like this in git-parse-remote.sh which makes
busybox ash unhappy:

                sed -ne '/^URL: */{
                        s///p
                        q
                }' "$GIT_DIR/remotes/$1"

It complains about "no previous regexp" while gnu sed is ok. Can
anyone explain to me what does "s///p" do? GNU Sed info page says
nothing about empty regexp. If I replace it with "s/\(.*\)/\1/p" then
I get "URL: " along with the remote path.

By the way, can we use another construct instead? It would be less
work for me ;-)
-- 
Duy

```

## Junio C Hamano, 2007-08-03 23:38

Subject: Re: Help a sed noob
Message-ID: <7vhcngkx38.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vhcngkx38.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <fcaeb9bf0708031622w25ab6e9ev61372169cfc0bd98@mail.gmail.com>

```
"Nguyen Thai Ngoc Duy" <pclouds@gmail.com> writes:

> There is a construct like this in git-parse-remote.sh which makes
> busybox ash unhappy:
>
>                 sed -ne '/^URL: */{
>                         s///p
>                         q
>                 }' "$GIT_DIR/remotes/$1"
>
> It complains about "no previous regexp" while gnu sed is ok. Can
> anyone explain to me what does "s///p" do? GNU Sed info page says
> nothing about empty regexp. If I replace it with "s/\(.*\)/\1/p" then
> I get "URL: " along with the remote path.

Traditionally empty LHS regexp means "the same as last match".
Replace it with "s/^URL: *//p" and you would be Ok.

```

## René Scharfe, 2007-08-03 23:40

Subject: Re: Help a sed noob
Message-ID: <46B3BCE7.3090509@lsrfire.ath.cx>
URL: https://gitlist.dev/e/46B3BCE7.3090509%40lsrfire.ath.cx
In-Reply-To: <fcaeb9bf0708031622w25ab6e9ev61372169cfc0bd98@mail.gmail.com>

```
Nguyen Thai Ngoc Duy schrieb:
> Hi,
> 
> There is a construct like this in git-parse-remote.sh which makes
> busybox ash unhappy:
> 
>                 sed -ne '/^URL: */{
>                         s///p
>                         q
>                 }' "$GIT_DIR/remotes/$1"
> 
> It complains about "no previous regexp" while gnu sed is ok. Can
> anyone explain to me what does "s///p" do? GNU Sed info page says
> nothing about empty regexp. If I replace it with "s/\(.*\)/\1/p" then
> I get "URL: " along with the remote path.
> 
> By the way, can we use another construct instead? It would be less
> work for me ;-)

You could manually add the previous regular expression, like this:

		sed -ne '/^URL: */{
			s/^URL: *//p
			q
		}' "$GIT_DIR/remotes/$1"

René

```

## Nguyen Thai Ngoc Duy, 2007-08-04 00:06

Subject: Re: Help a sed noob
Message-ID: <fcaeb9bf0708031706o3e3f4839t39eca4b64b0aab5d@mail.gmail.com>
URL: https://gitlist.dev/e/fcaeb9bf0708031706o3e3f4839t39eca4b64b0aab5d%40mail.gmail.com
In-Reply-To: <7vhcngkx38.fsf@assigned-by-dhcp.cox.net>

```
Great! Thank you both.

On 8/3/07, Junio C Hamano <gitster@pobox.com> wrote:
> "Nguyen Thai Ngoc Duy" <pclouds@gmail.com> writes:
>
> > There is a construct like this in git-parse-remote.sh which makes
> > busybox ash unhappy:
> >
> >                 sed -ne '/^URL: */{
> >                         s///p
> >                         q
> >                 }' "$GIT_DIR/remotes/$1"
> >
> > It complains about "no previous regexp" while gnu sed is ok. Can
> > anyone explain to me what does "s///p" do? GNU Sed info page says
> > nothing about empty regexp. If I replace it with "s/\(.*\)/\1/p" then
> > I get "URL: " along with the remote path.
>
> Traditionally empty LHS regexp means "the same as last match".
> Replace it with "s/^URL: *//p" and you would be Ok.
>
>


-- 
Duy

```

## martin f krafft, 2007-08-04 10:31

Subject: Re: Help a sed noob
Message-ID: <20070804103126.GA28402@lapse.madduck.net>
URL: https://gitlist.dev/e/20070804103126.GA28402%40lapse.madduck.net
In-Reply-To: <fcaeb9bf0708031622w25ab6e9ev61372169cfc0bd98@mail.gmail.com>

```
also sprach Nguyen Thai Ngoc Duy <pclouds@gmail.com> [2007.08.04.0122 +0200]:
>                 sed -ne '/^URL: */{
>                         s///p
>                         q
>                 }' "$GIT_DIR/remotes/$1"

Looks like it prints the first matching line:

  grep '^URL: *' | head -1

-- 
martin;              (greetings from the heart of the sun.)
  \____ echo mailto: !#^."<*>"|tr "<*> mailto:" net@madduck
 
quantum mechanics: the dreams stuff is made of.
 
spamtraps: madduck.bogus@madduck.net

```

## Jeff King, 2007-08-04 11:50

Subject: Re: Help a sed noob
Message-ID: <20070804115003.GA9716@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20070804115003.GA9716%40coredump.intra.peff.net
In-Reply-To: <20070804103126.GA28402@lapse.madduck.net>

```
On Sat, Aug 04, 2007 at 12:31:26PM +0200, martin f krafft wrote:

> also sprach Nguyen Thai Ngoc Duy <pclouds@gmail.com> [2007.08.04.0122 +0200]:
> >                 sed -ne '/^URL: */{
> >                         s///p
> >                         q
> >                 }' "$GIT_DIR/remotes/$1"
> 
> Looks like it prints the first matching line:
> 
>   grep '^URL: *' | head -1

No, it prints everything _except_ "URL: *" from the first matching line.

-Peff

```

## martin f krafft, 2007-08-04 12:18

Subject: Re: Help a sed noob
Message-ID: <20070804121811.GA1605@lapse.madduck.net>
URL: https://gitlist.dev/e/20070804121811.GA1605%40lapse.madduck.net
In-Reply-To: <20070804115003.GA9716@coredump.intra.peff.net>

```
also sprach Jeff King <peff@peff.net> [2007.08.04.1350 +0200]:
> No, it prints everything _except_ "URL: *" from the first matching line.

I am convinced we could submit this to the Obfuscated Code Contest.
:)

The behaviour is absolutely unclear from the manpage and defies my
logic. Can you elaborate a bit, even though this is off-topic?

-- 
martin;              (greetings from the heart of the sun.)
  \____ echo mailto: !#^."<*>"|tr "<*> mailto:" net@madduck
 
kill ugly radio
                                                        -- frank zappa
 
spamtraps: madduck.bogus@madduck.net

```

## Simon 'corecode' Schubert, 2007-08-04 12:26

Subject: Re: Help a sed noob
Message-ID: <46B4705D.2010208@fs.ei.tum.de>
URL: https://gitlist.dev/e/46B4705D.2010208%40fs.ei.tum.de
In-Reply-To: <20070804121811.GA1605@lapse.madduck.net>

```
martin f krafft wrote:
> also sprach Jeff King <peff@peff.net> [2007.08.04.1350 +0200]:
>> No, it prints everything _except_ "URL: *" from the first matching line.
> 
> I am convinced we could submit this to the Obfuscated Code Contest.
> :)

This is basic sed scripting.

> The behaviour is absolutely unclear from the manpage and defies my
> logic. Can you elaborate a bit, even though this is off-topic?

-ne means "don't print, execute the following code".

/.../ should be clear, { starts a "function list"
s// is "match last regexp, so s/// is "match last regexp and replace with empty string.
flag "p" to function "s" means "print if matched"
q is "quit".

that's really straight forward and all documented in my sed(1).

cheers
  simon

-- 
Serve - BSD     +++  RENT this banner advert  +++    ASCII Ribbon   /"\
Work - Mac      +++  space for low €€€ NOW!1  +++      Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \

```

## Jeff King, 2007-08-04 12:40

Subject: Re: Help a sed noob
Message-ID: <20070804124005.GA15334@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20070804124005.GA15334%40sigill.intra.peff.net
In-Reply-To: <20070804121811.GA1605@lapse.madduck.net>

```
On Sat, Aug 04, 2007 at 02:18:11PM +0200, martin f krafft wrote:

> The behaviour is absolutely unclear from the manpage and defies my
> logic. Can you elaborate a bit, even though this is off-topic?

The original sed code in question was:

                sed -ne '/^URL: */{
                        s///p
                        q
                }' "$GIT_DIR/remotes/$1"

There are a few things to note:
  1. -n means "do not print lines by default"
  2. sed addresses consist of an address (in this case a regex meaning
     "do this for lines that match the regex") and a command
  3. The braces start a set of commands, so that for lines matching the
     address, we do all of the commands.
  4. An empty matching portion for a regex means "use the last regex".

So this script comes down to:
  - don't write any lines except the ones we match
  - find a line that starts with URL:
    - replace the URL: part with nothing
    - print the result
    - quit

It could be more simply written as:

sed -ne 's/^URL: *//pq'

which uses the substitution as an address, but I don't know whether that
was allowed in the original sed.

-Peff

```

## martin f krafft, 2007-08-04 13:56

Subject: Re: Help a sed noob
Message-ID: <20070804135630.GC1605@lapse.madduck.net>
URL: https://gitlist.dev/e/20070804135630.GC1605%40lapse.madduck.net
In-Reply-To: <46B4705D.2010208@fs.ei.tum.de>

```
also sprach Simon 'corecode' Schubert <corecode@fs.ei.tum.de> [2007.08.04.1426 +0200]:
> s// is "match last regexp, so s/// is "match last regexp and replace with 
> empty string.

This fact is not in the manpage, AFAICT, at least not on Debian
systems.

-- 
martin;              (greetings from the heart of the sun.)
  \____ echo mailto: !#^."<*>"|tr "<*> mailto:" net@madduck
 
due to lack of interest tomorrow has been cancelled.
 
spamtraps: madduck.bogus@madduck.net

```

## David Kastrup, 2007-08-04 14:06

Subject: Re: Help a sed noob
Message-ID: <85abt7gzs1.fsf@lola.goethe.zz>
URL: https://gitlist.dev/e/85abt7gzs1.fsf%40lola.goethe.zz
In-Reply-To: <20070804135630.GC1605@lapse.madduck.net>

```
martin f krafft <madduck@madduck.net> writes:

> also sprach Simon 'corecode' Schubert <corecode@fs.ei.tum.de> [2007.08.04.1426 +0200]:
>> s// is "match last regexp, so s/// is "match last regexp and replace with 
>> empty string.
>
> This fact is not in the manpage, AFAICT, at least not on Debian
> systems.

IIRC, this is a traditional _ed_ feature.  I don't think it has ever
been a traditional part of _sed_, but maybe some implementations have
adopted this usage.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

```
