[comp.sys.amiga.tech] grep-ing for "*" with SKsh

alfredo@ajahnv.lonestar.org (Alfredo Jahn V) (07/16/90)

With SKsh 1.3 I used to be able to issue the command: grep "*" s:crontab
to pull out all lines with a "*" character.  Now I get a "grep: invalid 
expresion" error.  I tried quoting the "*" with:  \*  or '*'  but it 
doesn't work!  Could someone please email me the correct incantation?  
I can't seem to find it in the docs.

Thanks in advance,

 - alfredo

--------------------------------//----------------------------------------
 Alfredo Jahn V                //   Internet: alfredo@ajahnv.lonestar.org  
 3208 Cole Ave., #1303     \\ //    UUCP:     texbell!ajahnv!alfredo
 Dallas, Texas 75204  USA   \X/     Voice:    +1 214 855-1316 
--------------------------------------------------------------------------

koren@hpfelg.HP.COM (Steve Koren) (07/17/90)

> With SKsh 1.3 I used to be able to issue the command: grep "*" s:crontab
> to pull out all lines with a "*" character.  Now I get a "grep: invalid 

I doubt you were doing that with 1.3, since there was no grep command
in SKsh 1.3.  Perhaps you were using the 1.3 SKsh binary with the
1.4 external commands?

At any rate, there are two ways to do this.  The first (and probably
best) is to use fgrep.  Fgrep searches for strings only and not
regular expressions as grep does.  Hence, the '*' is not a special
character to fgrep and it will search for it as you would expect.
In addition, fgrep is about 3 times as fast and smaller.  Note that
you must still escape this character since it is a valid shell
wildcard character.  Something like this should work:

    fgrep '*' myfiles
or
    fgrep \* myfiles

The problem with using "grep" to search for the '*' is that the '*'
is a "special" regular expression character meaning "0 or more of the
previous expression".  It is also a special character to the shell,
used in pattern matching.  You must escape both of these to use the
'*' with grep.  Fgrep is easier.  If you normally find yourself
using fgrep instead of grep, you can alias one to the other.

The SKsh manual entry for "grep" does not explain regular expressions.
Should I put it in there?  Perhaps it would be of help to people
not familar with them.

   - steve

koren@hpfelg.HP.COM (Steve Koren) (07/18/90)

Earlier I wrote this:

> The problem with using "grep" to search for the '*' is that the '*'
> is a "special" regular expression character meaning "0 or more of the
> previous expression".  It is also a special character to the shell,
> used in pattern matching.  You must escape both of these to use the
> '*' with grep.  Fgrep is easier.  If you normally find yourself
> using fgrep instead of grep, you can alias one to the other.

I forgot to include the exact command line to grep:

  grep '\\*' myfile

The first backslash escapes the second in SKsh.  Then, a single
backslash gets passed to grep which grep uses to escape the
'*' which is a special character.

   - steve