git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Help a sed noob
@ 2007-08-03 23:22 Nguyen Thai Ngoc Duy
  2007-08-03 23:38 ` Junio C Hamano
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2007-08-03 23:22 UTC (permalink / raw)
  To: Git Mailing List

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-03 23:22 Help a sed noob Nguyen Thai Ngoc Duy
@ 2007-08-03 23:38 ` Junio C Hamano
  2007-08-04  0:06   ` Nguyen Thai Ngoc Duy
  2007-08-03 23:40 ` René Scharfe
  2007-08-04 10:31 ` martin f krafft
  2 siblings, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2007-08-03 23:38 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: Git Mailing List

"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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-03 23:22 Help a sed noob Nguyen Thai Ngoc Duy
  2007-08-03 23:38 ` Junio C Hamano
@ 2007-08-03 23:40 ` René Scharfe
  2007-08-04 10:31 ` martin f krafft
  2 siblings, 0 replies; 11+ messages in thread
From: René Scharfe @ 2007-08-03 23:40 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: Git Mailing List

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é

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-03 23:38 ` Junio C Hamano
@ 2007-08-04  0:06   ` Nguyen Thai Ngoc Duy
  0 siblings, 0 replies; 11+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2007-08-04  0:06 UTC (permalink / raw)
  To: Junio C Hamano, René Scharfe; +Cc: Git Mailing List

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-03 23:22 Help a sed noob Nguyen Thai Ngoc Duy
  2007-08-03 23:38 ` Junio C Hamano
  2007-08-03 23:40 ` René Scharfe
@ 2007-08-04 10:31 ` martin f krafft
  2007-08-04 11:50   ` Jeff King
  2 siblings, 1 reply; 11+ messages in thread
From: martin f krafft @ 2007-08-04 10:31 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: Git Mailing List

[-- Attachment #1: Type: text/plain, Size: 525 bytes --]

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

[-- Attachment #2: Digital signature (see http://martin-krafft.net/gpg/) --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-04 10:31 ` martin f krafft
@ 2007-08-04 11:50   ` Jeff King
  2007-08-04 12:18     ` martin f krafft
  0 siblings, 1 reply; 11+ messages in thread
From: Jeff King @ 2007-08-04 11:50 UTC (permalink / raw)
  To: martin f krafft; +Cc: Nguyen Thai Ngoc Duy, Git Mailing List

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-04 11:50   ` Jeff King
@ 2007-08-04 12:18     ` martin f krafft
  2007-08-04 12:26       ` Simon 'corecode' Schubert
  2007-08-04 12:40       ` Jeff King
  0 siblings, 2 replies; 11+ messages in thread
From: martin f krafft @ 2007-08-04 12:18 UTC (permalink / raw)
  To: Jeff King; +Cc: Nguyen Thai Ngoc Duy, Git Mailing List

[-- Attachment #1: Type: text/plain, Size: 611 bytes --]

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

[-- Attachment #2: Digital signature (see http://martin-krafft.net/gpg/) --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-04 12:18     ` martin f krafft
@ 2007-08-04 12:26       ` Simon 'corecode' Schubert
  2007-08-04 13:56         ` martin f krafft
  2007-08-04 12:40       ` Jeff King
  1 sibling, 1 reply; 11+ messages in thread
From: Simon 'corecode' Schubert @ 2007-08-04 12:26 UTC (permalink / raw)
  To: Jeff King, Nguyen Thai Ngoc Duy, Git Mailing List

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   / \

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-04 12:18     ` martin f krafft
  2007-08-04 12:26       ` Simon 'corecode' Schubert
@ 2007-08-04 12:40       ` Jeff King
  1 sibling, 0 replies; 11+ messages in thread
From: Jeff King @ 2007-08-04 12:40 UTC (permalink / raw)
  To: Git Mailing List

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-04 12:26       ` Simon 'corecode' Schubert
@ 2007-08-04 13:56         ` martin f krafft
  2007-08-04 14:06           ` David Kastrup
  0 siblings, 1 reply; 11+ messages in thread
From: martin f krafft @ 2007-08-04 13:56 UTC (permalink / raw)
  To: Simon 'corecode' Schubert
  Cc: Jeff King, Nguyen Thai Ngoc Duy, Git Mailing List

[-- Attachment #1: Type: text/plain, Size: 487 bytes --]

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

[-- Attachment #2: Digital signature (see http://martin-krafft.net/gpg/) --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help a sed noob
  2007-08-04 13:56         ` martin f krafft
@ 2007-08-04 14:06           ` David Kastrup
  0 siblings, 0 replies; 11+ messages in thread
From: David Kastrup @ 2007-08-04 14:06 UTC (permalink / raw)
  To: Simon 'corecode' Schubert
  Cc: Jeff King, Nguyen Thai Ngoc Duy, Git Mailing List

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2007-08-04 14:06 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-08-03 23:22 Help a sed noob Nguyen Thai Ngoc Duy
2007-08-03 23:38 ` Junio C Hamano
2007-08-04  0:06   ` Nguyen Thai Ngoc Duy
2007-08-03 23:40 ` René Scharfe
2007-08-04 10:31 ` martin f krafft
2007-08-04 11:50   ` Jeff King
2007-08-04 12:18     ` martin f krafft
2007-08-04 12:26       ` Simon 'corecode' Schubert
2007-08-04 13:56         ` martin f krafft
2007-08-04 14:06           ` David Kastrup
2007-08-04 12:40       ` Jeff King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).