5.8 Regular expressions and grep |
grep
is a good way to introduce regular expressions. Regular
Expressions, or re, are used by many programs. It is the use
of special characters to represent general characters. For
example, the use of an asterisk "*" to represent any possible
string of characters as part of a file name when "talking" to a shell.
With regular expressions you will also have a way of escaping from
the special meaning of a special character such as the asterisk "*".
This way you will still be able to search for file names that contain
asterisks. (This is NOT recommended.) The escape sequence
is to place a backslash in front of the special character. This
also works for the backslash.
grep
is a fast way to search the content of a file.
Please look at the manual pages for grep. The manual pages
will give you more information on switches and how to use grep.
I have used grep many, many times. Two examples come to mind that have been very useful. I will outline these uses.
The first was to find files on floppy disks, after I had a hard disk.
At this time I had several hundred floppy disks. I wrote a program
that would do a recursive directory search of a disk creating a file
of the disks content and printing a disk label. I also ran this program
on the hard disks of systems that I was using at the time.
The name of the file
was the number on the printed label (for floppy disks).
The content of the file was
for each file on the disk one line. This line included the disk number,
the full path name of the file, and the
creation/modification time of the file.
If I modified a disk I could re-run the program recreating a new directory
file, overwriting the old entry.
I placed all of the resulting
files in a single directory on my hard disk.
I could then use grep
to search all of the files for a file name.
grep
s output would tell me what disk had the file, when it was last
modified, and what directory the file was in.
The second example relates to maintenance programming. I was involved in a writing and updating some product test code for some multi-bus products here in Tucson. It was a summer job. The code was all new to me. I needed to be able to change sections of the "C" code that was broken up into about 60 different files, both program and header files.
I used grep
to search all of the modules for references to
a function names, variable names, constants - both symbolic and numeric,
file names and other constructs. It allowed me to quickly
get a grasp of where "things" where both declared and used. It
also let me verify that I had made changes in all of the needed locations.
5.8 Regular expressions and grep |