This is the mail archive of the
mailing list for the Cygwin project.
RE: grep -P regexp problem
- From: "Phil Betts" <Phil dot Betts at ascribe dot com>
- To: <cygwin at cygwin dot com>
- Date: Wed, 4 Mar 2009 13:54:29 -0000
- Subject: RE: grep -P regexp problem
- References: <BAY127-DS374577AD66B9A49AF63E9A6A60@phx.gbl>
- Reply-to: <cygwin at cygwin dot com>
Andriy Sen wrote:
> Below is an example of the problem.
> G:\>cat test.s
> G:\>cat test.s | grep -P "[^0]1"
This is not cygwin-specific, so it is really OT for this list, that
grep -P treats the whole input as a single string, and outputs the
line (or lines) containing the match for the pattern. [^0] matches
ANYTHING except 0, including linefeeds.
In your case, the [^0] is matching the linefeed preceding the 1. That
linefeed is considered part of the line "a\n", so that line is
included in the output. In other words, although it looks like there
are two matches output, in fact there is only one match, and that is
Assuming you wish to match single lines containing a character other
than 0 followed by a 1, you probably want the pattern to be '[^0\n]1'
It's probably a bit clearer if the test file is a bit bigger:
$ echo -e 'a\n1\n2\n3\n4\n1\n2\n21\n' > test.txt
$ grep -P '[^0]1' test.txt
This output contains 3 matches "a\n1\n" "4\n1\n" and "21\n", whereas:
$ grep -P '[^0\n]1' test.txt
only matches single lines with a 1 that follows anything but 0.
This email has been scanned by Ascribe PLC using Microsoft Antigen for Exchange.
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html