[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug #25765] grep -P regexp also prints the line before the match
From: |
Wacek Kusnierczyk |
Subject: |
Re: [bug #25765] grep -P regexp also prints the line before the match |
Date: |
Thu, 05 Mar 2009 08:57:01 +0100 |
User-agent: |
Thunderbird 2.0.0.19 (X11/20090105) |
Wacek Kusnierczyk wrote:
> Andriy Sen wrote:
>
>> See tree examples below. The last one shows the problem.
>>
>>
>> G:\>cat test.s
>> a
>> 1
>>
>> G:\>od -a test.s
>> 0000000 a nl 1 nl
>> 0000004
>>
>> G:\>cat test.s | grep -P "^[^0]1"
>>
>>
>> G:\>cat test.s | grep -P "^(|[^0])1"
>> 1
>>
>> G:\>cat test.s | grep -P "^(|.*[^0])1"
>> a
>> 1
>>
>>
>>
>
> what problem?
>
> cat test.s | grep -Pn "^(|.*[^0])1"
> # 1:a
> # 1
>
> as you can see, there is one match here. '.*' matches 'a', and '[^0]'
> matches the newline. and so '1' can match '1'. there is no 'line
> before the match'; the line before that containing '1' is *within* the
> match.
>
to follow up, that's what you'd see in perl:
"a\n1\n" =~ /^(?:|.*[^0])1/
evaluates to 1 (true).
but on reflection, i'd agree there is a sort-of problem, in that man
grep says (emphasis added):
" grep searches the named input FILEs (or standard input if no files are
named, or if a single hyphen-minus (-) is given as file name)
*for lines*
containing a match to the given PATTERN. By default, grep
prints the
matching lines."
clearly, your test.s does not contain any single *line* that would match
the pattern -- but that's a problem with the documentation, i'd think,
which hasn't been updated to accommodate for the -P option which can
cause grep to match *across* lines, as above, and as here:
grep '.\n.' test.s
# no match
grep -P '.\n.' test.s
# match
i would certainly not consider this a bug, though.
vQ