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

RE: A note on the use of grep [WAS: Re: HELP! My printer won't stop!!]

Quoting Michael Sims <michaels@crye-leike.com>:

> roberto@familiasanchez.net wrote:
> >>> ps aux | grep [l]pr
> >>> 
> >>> This still lists all the processes that contain the string "lpr",
> >>> but it will not match the grep process itself anymore.
> >> 
> >> Why is that?  Isn't a bracket expression containing only one
> >> character exactly the same as the character by itself?  Am I missing
> >> something blindingly obvious? :) 
> > 
> > As you point out, the brackets with one character amount to a range
> > of one character.  The "[l]pr" regexp is intrepreted as "lpr", but
> > the grep command show up in ps as "grep [l]pr".  This prevents grep
> > from matching its own process as it is output by ps.
> </me slaps forehead>
> Thanks, guess I didn't think hard enough. :)  Nice trick, BTW...

>From the grep info manual:

  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'.  Note that some platforms
     `ps' limit the ouput to the width of the screen, grep does not
     have any limit on the length of a line except the available memory.

This message was sent using IMP, the Internet Messaging Program.

Reply to: