Getting this simple regular expression to match in grep

Solution 1:

It looks like you're using Windows Command Prompt (cmd.exe) as your shell, and you're getting tripped up by its quoting conventions, or lack thereof. If I run your command in Fedora 15 Bash shell, it works. If I run it in Windows using Cygwin's Bash shell, it works.

To get it to work with cmd.exe, you have to change the quotes and spacing. I ran the commands below in cmd.exe on Windows 7. Note how I changed the quotes on the grep command to use single quotes instead of double quotes, and there is no space before the pipe (|).

I am using the Cygwin version of GNU grep, which should behave the same as your Win32 GNU grep.

c:\>c:\cygwin\bin\grep --v
GNU grep 2.6.3

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

c:\>echo "2008abc.html"| c:\cygwin\bin\grep -oiP '\"[^.]'
"2

If there is a space before the pipe, the space will be echoed through the pipeline and grep will match it. This is due to the idiotic parsing behavior of cmd.exe.

c:\>echo "2008abc.html" | c:\cygwin\bin\grep -oiP '\"[^.]'
"2
"

For your own sanity, see if you can use Cygwin's Bash or any other shell with reasonable and consistent quoting conventions.