On Saturday 24 March 2001 14:14, you wrote:
> Hi list,
>
> I know I've used this utility before, but you know what?...I really have
> no idea for sure what the little bugger is. I've taken it for granted
> that it's in there and used it as I've seen it exampled.
>
> So, could someone provide a definitive explanation of just what exactly
> grep is, where I might find some real thorough information on usage and
> function on this wonder of hidden technology?
>
> thanks,
>
> Mark
Hiya Mark!
Well there are as many uses for grep as there are for a pocket camera.
Generalized Regular Expression Parser
Basically it returns each line or each item in a list that matches a regular
expression that you give it.
Regular expressions? Well there are three frequent styles of them,
recognized with slight differences by different programs. Here are some
examples:
[A-Z][a-z] match any alphabetic
^ match begining of line
* match zero or more preceding
. match any character
$ match end of line
\ makes an "escape" so the next character is not a
metacharacter
+ match one or more preceding
? match zero or more preceding
| either/or choices to match
() groups an expression to match (where it would otherwise
be unclear)
You can also store a pattern for later replay but occasions to use that in
day-to-day system administration are rare.
And to make things more fun, there are variations of grep.
rgrep is a recursive grep through files in a directory and possibly
subdirectories.
fgrep searches a bunch of files, and prints lines that match or don't match
text and possibly n lines before and m lines after along with the file name
and line number--a handy little concordance generator.
egrep does the same as fgrep but supports different regular expressions and
doesn't support pattern storage or \< \> \n \| escapes.
Usually one doesn't use grep by itself, but there are some times where it
might be helpful. I had just installed StarOffice for a bunch of secretaries
and they could not find their files and I hadn't studied that portion of
StarOffice... So I asked "Who is the letter addressed to?" Then
rgrep -l "Stevens" /home/karmen
popped up the directory path to the file.
Or suppose you want to remove all the packages whose names begin with 'kde'
You can get a list of those packages with
rpm -qa | grep ^kde
since ` paired with another ` on the bash command line means "Subsitute the
output of the program" and since most commands are happy to take a list of
files,
rpm -e `rpm -qa | grep ^kde`
will attempt to remove them all (of course it might encounter a dependency
problem).
Or suppose I wanted a list of non-mandrake rpm packages on a system (typical
bug analysis task)
rpm -qa | grep -v [0-9]*mdk
so, the basis of grep is a pattern-matching engine that searches files
looking for matches on a line, and doing something with those lines that it
is not doing to other lines (or vice versa) exactly what it does and whether
it does that to the lines that match or the lines that don't match is
controlled by the many option switches available. The other program names in
the grep family are basically just built-in option switches.
Civileme