funny grep behavior

  • Follow


Ok, here's some funny behavior by grep ...

$ cat file
cmd[2]
25300
25400
$ grep 25* file  <-- looking for lines having "25" in them
cmd[2]
25300
25400
$ grep 2* file  <-- looking for lines having "2" in them
cmd[2]
25300
25400
$ grep 5* file
cmd[2]
25300
25400
$ grep 3* file
cmd[2]
25300
25400
$ grep "3*" file
cmd[2]
25300
25400
$ uname -a    <-- just in case you're wondering
SunOS jgalt 5.9 Generic_112233-07 sun4u sparc SUNW,UltraAX-i2

Now this is strange. I am really only looking for lines
that have "25" followed by something. But each time, grep returns
all the lines in the file.

But when I try

$ grep 253*
25300
25400
$

which is still wierd.

I'd really appreciate if there's a grep/regex guru out there who can
shed some light on what's going on.

thanks in advance,
John Galt
0
Reply johngalt__ 9/25/2003 6:12:07 AM

On Thu, 25 Sep 2003 at 06:12 GMT, John Galt wrote:
> Ok, here's some funny behavior by grep ...
> 
> $ cat file
> cmd[2]
> 25300
> 25400
> $ grep 25* file  <-- looking for lines having "25" in them
> cmd[2]
> 25300
> 25400
[snip]
> Now this is strange. I am really only looking for lines
> that have "25" followed by something. But each time, grep returns
> all the lines in the file.
> 
> But when I try
> 
> $ grep 253*
> 25300
> 25400
> $
> 
> which is still wierd.
> 
> I'd really appreciate if there's a grep/regex guru out there who can
> shed some light on what's going on.

    "grep 25*" looks for lines containing a 2 followed by 0 or more
    5s.

    In a regular expression, an asterisk means "match zero or more
    instances of the preceding character"; it is not the same as in
    file patterns, where an asterisk by itself means zero or more
    instances of any character.

    To search for 25:

grep 25 file

    To search for lines that start with 25:

grep ^25 file

    To search for line that contain 25 with at least one following
    character: 

grep '25.' file

    A period means "match any single character". A period followed by
    an asterisk means "match zero or more instances of any
    character".

    There's a lot more to them; try these pages for more information:

       <http://www.linuxfocus.org/English/July1998/article53.html>
       <http://www.amk.ca/python/howto/regex/>
       <http://etext.lib.virginia.edu/helpsheets/regex.html>

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Reply Chris 9/25/2003 6:29:06 AM


"Chris F.A. Johnson" <c.f.a.johnson@rogers.com> wrote in message news:<bku1vh$5vva9$1@ID-136730.news.uni-berlin.de>...
> On Thu, 25 Sep 2003 at 06:12 GMT, John Galt wrote:
> > Ok, here's some funny behavior by grep ...
> > 
> > $ cat file
> > cmd[2]
> > 25300
> > 25400
> > $ grep 25* file  <-- looking for lines having "25" in them
> > cmd[2]
> > 25300
> > 25400
> > $
> 
>     "grep 25*" looks for lines containing a 2 followed by 0 or more
>     5s.

AH. Ok it all makes sense now. Thanks Chris!

> 
>     In a regular expression, an asterisk means "match zero or more
>     instances of the preceding character"; it is not the same as in
>     file patterns, where an asterisk by itself means zero or more
>     instances of any character.
> 
>     To search for 25:
> 
> grep 25 file
> 
>     To search for lines that start with 25:
> 
> grep ^25 file
> 
>     To search for line that contain 25 with at least one following
>     character: 
> 
> grep '25.' file
> 
>     A period means "match any single character". A period followed by
>     an asterisk means "match zero or more instances of any
>     character".
> 
>     There's a lot more to them; try these pages for more information:
> 
>        <http://www.linuxfocus.org/English/July1998/article53.html>
>        <http://www.amk.ca/python/howto/regex/>
>        <http://etext.lib.virginia.edu/helpsheets/regex.html>
0
Reply johngalt__ 9/25/2003 1:38:05 PM

2 Replies
151 Views

(page loaded in 0.044 seconds)

Similiar Articles:






7/23/2012 11:05:10 AM


Reply: