grep-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Grep-devel] look ahead capture content :(


From: Jim Meyering
Subject: Re: [Grep-devel] look ahead capture content :(
Date: Sun, 10 Mar 2019 13:56:15 -0700

On Mon, Mar 4, 2019 at 8:26 AM Marc Chantreux <address@hidden> wrote:
> I would like to use grep instead of sed in this particular context
>
>     sed 's/^Channel \+//;t;d' <<\.
>     Channel foo
>     Rocket  bang
>     Channel bar
>     .
>
> the result is
>
>     foo
>     bar
>
> i expected the same result with
>
>     grep -Po '(?=^Channel +).*' <<\.
>     Channel foo
>     Rocket  bang
>     Channel bar
>     .
>
> but instead, i got
>
>     Channel foo
>     Channel bar
>
> is there a bug? a misuse of mine ?

+1 for using grep (and -P/--pcre).
However you appear to have confused look-behind and look-ahead.
Note how each of these works:

  look-ahead$ printf '%sz\n' a b c |grep -Po '.(?=z)'
  a
  b
  c
  look-behind$ printf 'a%s\n' x y z |grep -Po '(?<=a).'
  x
  y
  z

However, changing (?= to (?<= in your example would fail like this:

  grep: lookbehind assertion is not fixed length

If you want to use PCREs, consider just using perl:

  perl -nle 'm{^Channel +(.*)} and print $1'



reply via email to

[Prev in Thread] Current Thread [Next in Thread]