[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: grep question



On Mon, Apr 08, 2002 at 04:41:08PM +0200, Johann Spies wrote:
| In the grep's info page I find the following which works as said.  But
| I want to know why.  What does the [c] do in this case?
| 
| ------------------------
|   7. Why do people use strange regular expressions on `ps' output?
| 
|           ps -ef | grep '[c]ron'
| 
|      If the pattern had been written without the square brackets, it
|      would have matched not only the `ps' output line for `cron', but
|      also the `ps' output line for `grep'.
| 
| -------------------------------

That's kinda tricky.  Suppose you left the brackets out.  ps would
have given you lines with

    /usr/sbin/cron
    ps -ef | grep 'cron'

Note that both of these lines match the regex 'cron'.  If you include
the square brackets, you would have

    /usr/sbin/cron
    ps -ef | grep '[c]ron'

The square brackets indicate a character class.  In this usage, the
character class contains just one character, so as far as the regex is
concerned '[c]' is equivalent to 'c'.  However, the string "[c]ron"
is not matched by the regex '[c]ron' or 'cron'.  That's where the
tricky part comes in.  By changing the regex, you have changed the
output that ps will give so that it doesn't match the regex.  Yet you
didn't change the regex's semantics at all.

HTH,
-D

-- 

A man of many companions may come to ruin,
but there is a friend that sticks closer than a brother.
        Proverbs 18:24


-- 
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org



Reply to: