asterisk in regular expressions

  • Follow


Hello,

Why would command

sys1:~$ echo "now" | grep [w]*

yield "now" and command

sys1:~$ echo "now" | grep [n]*

yields nothing?  Shouldn't they both give the same output?

0
Reply struchki (1) 8/23/2010 5:17:30 AM

>>>>> "harums" == harums  <struchki@gmail.com> writes:

harums> Hello,
harums> Why would command

harums> sys1:~$ echo "now" | grep [w]*

harums> yield "now" and command

harums> sys1:~$ echo "now" | grep [n]*

harums> yields nothing?  Shouldn't they both give the same output?

What happens when you quote the arg to grep, which contains shell
metachars so it should be quoted.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
0
Reply merlyn1 (1433) 8/23/2010 5:29:17 AM


harums <struchki@gmail.com> writes:

> sys1:~$ echo "now" | grep [w]*
>
> yield "now" and command
>
> sys1:~$ echo "now" | grep [n]*
>
> yields nothing?  Shouldn't they both give the same output?

Remember that the shell will do glob expansion *before* invoking any of
the commands. The brackets ‘[]’ and asterisk ‘*’ are all globbing
characters to the shell. The arguments to ‘grep’ will depend on what
expansion resulted from those globs.

-- 
 \            “… Nature … is seen to do all things Herself and through |
  `\         herself of own accord, rid of all gods.” —Titus Lucretius |
_o__)                                                 Carus, c. 40 BCE |
Ben Finney
0
Reply unix49 (48) 8/23/2010 6:04:58 AM

On August 23, 2010 02:04, in comp.unix.questions, ben+unix@benfinney.id=
..au
wrote:

> harums <struchki@gmail.com> writes:
>=20
>> sys1:~$ echo "now" | grep [w]*
>>
>> yield "now" and command
>>
>> sys1:~$ echo "now" | grep [n]*
>>
>> yields nothing?  Shouldn't they both give the same output?
>=20
> Remember that the shell will do glob expansion *before* invoking any =
of
> the commands. The brackets =E2=80=98[]=E2=80=99 and asterisk =E2=80=98=
*=E2=80=99 are all globbing
> characters to the shell. The arguments to =E2=80=98grep=E2=80=99 will=
 depend on what
> expansion resulted from those globs.

Remember also that, should the shell not find any matches for it's glob=
bing,
it returns the globbing string.

Note the behaviour in the following example.

The directory is initially empty
  ~/tmp $ ls
  ~/tmp $ echo *
  *
and 'echo *' echoes an asterisk. But, populate the directory with a fil=
e,
  ~/tmp $ touch abc
  ~/tmp $ echo *
  abc
  ~/tmp $=20
and 'echo *' now echoes the filename.

Assume in the OP's ~ directory that there are no files that match '[w]*=
',
and there are files that match '[n]*', but none of those files are
called "now".

Then,
  sys1:~$ echo "now" | grep [w]*
will
  - echo "now" to stdout
  - glob '[w]*' matches no existing file in ~, so defaults to '[w]*'
  - grep successfully matches the stdin string "now" to the regexp '[w]=
*'

OTOH,=20
  sys1:~$ echo "now" | grep [n]*
will
  - echo "now" to stdout
  - glob '[n]*' matches an existing filename, and that filename is
    substituted into the grep commandline
  - grep unsuccessfully matches the stdin string "now" to the file name=
 list
    derived from the expansion of the regexp '[n]*' within the CWD.

To test this theory, let's try another example.

Again, we start with an empty directory, and try the OP's regexp
  ~/tmp $ ls
  ~/tmp $ echo "now" | grep [w]*
  now
and we get the OP's results.

Now, let's create an appropriately named file in the directory
  ~/tmp $ touch wood
  ~/tmp $ ls
  wood

and try the test again
  ~/tmp $ echo "now" | grep [w]*
  ~/tmp $=20
Now we see the OP's second (to him, aberant) results.

Let's see what grep sees in this second example...
  ~/tmp $ echo grep [w]*
  grep wood
  ~/tmp $=20

So, in the 1st example, grep is given a regexp that it can match to the=

input data, and succeeds. But, in the 2nd example, grep is given a list=
 of
one or more string literals (filenames) that it cannot match to the inp=
ut
data, thus it fails.

HTH
--=20
Lew Pitcher
Master Codewright & JOAT-in-training   | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.c=
a/
----------      Slackware - Because I know what I'm doing.         ----=
--


0
Reply lpitcher2 (869) 8/23/2010 10:24:21 AM

On 23 Aug, 06:17, harums <struc...@gmail.com> wrote:

> sys1:~$ echo "now" | grep [n]*
> yields nothing? =A0Shouldn't they both give the same output?

See replies from others, that results depend on any filenames starting
with 'n' in the directory, and quotes will help.
(A colleague pointed out that in fact if there were two files start
with 'n', say n1 and n2, they would be used as parameters to grep, to
search for string "n1" in file n2).

Also, note that even with quotes, the part    grep [n]*
might not do what you want anyway, since the asterisk means search for
zero-or-more occurences of the letter n

So, for example:
  echo then | grep "[w]*"
will display
  then

Mark
0
Reply mark.bergman (65) 8/27/2010 1:04:16 PM

4 Replies
23 Views

(page loaded in 0.133 seconds)

5/21/2013 9:47:29 PM


Reply: