Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strange grep behavior

19 views
Skip to first unread message

kumbh...@gmail.com

unread,
Mar 28, 2013, 10:27:01 PM3/28/13
to

All my life I thought grep "foo*" means "foo" with something (which includes nothing) after it.

But this has me stumped:

$ cat f
uid=003
uid=0034
uid=0045
$ grep "uid=1" f
$ grep "uid=1*" f
uid=003
uid=0034
uid=0045
$ grep "uid=1." f
$ grep "uK*" f
uid=003
uid=0034
uid=0045
$ uname -a
SunOS zoomon 5.9 Generic_122300-61 sun4u sparc SUNW,

"uK*" and "uid=1*" should return nothing, right? Am I missing something so totally obvious (after 10 years of programming)? Or is God playing dice just because He can?

thanks,
VK


Ben Bacarisse

unread,
Mar 28, 2013, 11:18:44 PM3/28/13
to
Did you read the manual? That's where I start when I get stumped. I
know I should start there *before* I get stumped but I do get round to
it when faced with a puzzle.

--
Ben.

Paul

unread,
Mar 29, 2013, 12:16:12 AM3/29/13
to
I don't know the answer, but this looks like fun.

http://www.zytrax.com/tech/web/regex.htm

There are some blue boxes, half way down the page. You
can type in your regex and test it. And apparently, the
regex is evaluated by Javascript in your browser (not by
the web server). It seems to be telling me, what portion
of the string matched my criterion, so gives more info
than a simple command line test would have on my computer.

http://img707.imageshack.us/img707/420/regex.gif

I tried your test cases, and got the same result as you did.
Obviously, the punctuation in that expression, have
some "regex meaning", but exactly what, I can't figure it out.

If I try "uK*", that matches on "u" at position 1.
I think K* means "match K, zero or more times".
And "u" meets the criterion, by having K occur
zero times after it.

Paul
Message has been deleted

James K. Lowden

unread,
Mar 29, 2013, 10:25:31 AM3/29/13
to
On Thu, 28 Mar 2013 19:27:01 -0700 (PDT)
kumbh...@gmail.com wrote:

> All my life I thought grep "foo*" means "foo" with something (which
> includes nothing) after it.

man re_format :-)

"An atom followed by '*' matches a sequence of 0 or more
matches of the atom. An atom followed by '+' matches a sequence of 1
or more matches of the atom."

The regular expression foo* means " 'fo' followed by zero or more 'o'
characters".

HTH.

--jkl

hymie!

unread,
Mar 29, 2013, 1:45:31 PM3/29/13
to
In our last episode, the evil Dr. Lacto had captured our hero,
kumbh...@gmail.com, who said:
>
>All my life I thought grep "foo*" means "foo" with something (which
>includes nothing) after it.

There are, essentially, two different situations here.

The shell indeed interprets "foo*" as "foo with something (which includes
nothing) after it." But you are not using shell expansion here.

A regular expression interprets "foo*" as "fo followed by 0 or more o's".

> $ grep "uid=1" f
> $ grep "uid=1*" f

"uid= followed by 0 or more 1's"
>uid=003
>uid=0034
>uid=0045

--hymie! http://lactose.homelinux.net/~hymie hy...@lactose.homelinux.net
-------------------------------------------------------------------------------
0 new messages