home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Big Blue Disk 20
/
bbd20.zip
/
GREP.TXT
< prev
next >
Wrap
Text File
|
1988-03-29
|
7KB
|
131 lines
|D╔══════════════════╗════════════════════════════════════════════════════════════
|D║ |5The Happy Hacker |D║════════════════════════════════════════════════════════════
|D╚══════════════════╝════════════════════════════════════════════════════════════
^C^1GREP
^Cby
^CDaniel Tobias
With a name like "Grep", it has to be good. For years, this rather peculiar
name has denoted a very useful command to the UNIX community. GREP lets you
search through a file or many files, looking for a particular pattern. Let's
say you vaguely remember that one of the text files on a disk had a reference
to ducks that you'd like to find. So, type "grep duck *.txt", and a list of
all the lines with that word in any file with a .TXT extension will pop up.
Even more complex things can be done, such as searching for patterns of
characters more complex than that. The syntax is as follows:
^1grep [options] expression [files]
where options (if present) is a hyphen (-) followed by a list of letters
controlling some parameters of GREP, expression is the target pattern to look
for, and files is a list of files (possibly including drive, path, and
wildcards) separated by spaces, through which to search. If the files are
omitted, standard input is used; you would then have to type in the lines to be
searched from the keyboard instead of getting them from a file, ending with
CTRL-Z and ENTER. This is not particularly useful by itself, but might be handy
in a more complex expression using DOS I/O redirection or piping. (If you don't
know what these things are, ignore this and always use a filename when typing a
GREP expression.)
The normal result of GREP is to output all lines in the given file(s) which
contain the given expression. If multiple files are selected, the name of the
file is shown with each line. However, the effects of GREP can be modified by
the options below.
The option letters you can use are:
v Output all lines EXCEPT those which match.
c Output only a count of matching lines, not the lines themselves.
i Ignore upper/lowercase distinction when matching.
l Output only the names of the files in which matches are found.
n Precede each line by its relative line number in the file.
s Suppress the error messages given for nonexistent files.
d Recursively search subdirectories below current one as well.
Hence, ^1grep -vi test junk.txt^0 outputs only those lines in JUNK.TXT that do
not contain the string "test", ignoring case distinction: "TEST" is equivalent.
The search expression syntax is as follows:
1. An ordinary character (other than a period, asterisk, left square bracket,
backslash, caret, dollar sign, or curly brace) matches itself. Hence, the
letter "t" in a search expression tells that you are looking for the letter "t".
2. A backslash followed by one of the above-mentioned characters matches the
following character. This is the only way to do a search for one of these
reserved characters. For example, "\\" matches a backslash.
3. A period (.) matches any character.
4. A (non-empty) string of characters within square brackets matches any of
the characters within the brackets. Ranges can be indicated using the hyphen.
Hence, "[abc]" will match the letter a, b, or c; [a-jx] will match any letter
from a to j, or x. Order of characters follows standard ASCII.
If the first character within the brackets is a caret (^^), the meaning is
reversed: it matches anything EXCEPT the characters within the brackets.
5. Any of the preceding followed by an asterisk (*) matches zero or more
occurrences of the preceding item. Hence, "b*" matches "" (null string), "b",
"bb", "bbb", etc. "[a-c]*" matches any number of occurrences of the letters a
through c, with no other characters intervening.
6. Any of the first four items followed by {m,n} (where m and n are integers
less than 256) matches between m and n consecutive occurrences of the preceding
item. Hence, "[a-j]{2,12}" matches 2 through 12 occurrences of letters in the
range of a through j. A single number like {m} means to match exactly m
occurrences; a number followed by a comma like {m,} matches m or more occur-
rences. "q{23,}" matches 23 or more consecutive appearances of the letter q.
7. A string of any of the above items in a row matches a sequence of the
things they match, consecutively. For instance, "ab*cd[efg]" matches the
letter a followed by zero or more occurrences of the letter b, followed by the
letters c and d, and one of e, f, or g. (e.g. "abbbcdf" or "acdg")
8. In general, an expression can match a substring anywhere within a line
in a file. However, if the first character of the expression is a caret (^^),
then the substring must be at the start of a line, while if the expression
ends with a dollar sign ($), it must be at the end. Hence, "^^abc" matches the
string "abc" only if it occurs at the beginning of a line; "^^def$" matches the
string "def" only if it is an entire line by itself.
Some applications: If you have a list of friends' names and phone numbers
in a file PHONE.DAT, the command
^Cgrep -i john phone.dat
will give you the line(s) containing the phone numbers of anyone named John or
Johnson. (The -i is there to ignore upper and lower case distinctions.)
If you want to know how often the word "indisputably" is used in the text
file VERBOSE.TXT, the command
^Cgrep -ci indisputably verbose.txt
will tell you. (Note that only the number of lines containing the word is
given, not necessarily the number of times the word appears. If the word
appears more than once in a line, the number will be underreported.)
If you're trying to find "inalienable" or "unalienable" in the Declaration
of Independence (but you're not sure which), you might try
^Cgrep [ui]nalienable declarat.ion
To look for a text line containing the word "whatever" followed somewhere
later by the word "want", with arbitrary text in between, try
^Cgrep whatever.*want *.txt
If you forgot where on your hard disk you had a file that talked about
Joe Finkelstein, you can type
^Cgrep -id finkelstein \*.*
There are lots of other applications. Use GREP any time you need to find
something that's buried in a disk file.
Please note that this program runs faster the fewer special characters (like
the asterisk) you use. Searches for a pure character string with no special
stuff go fairly fast, while searches for a string beginning with ".*" (worst
case) can be very slow. Plan your input accordingly. Also, of course, the more
files you search the longer it will take. Narrow it down if possible by
selecting a subdirectory and extension, like \subdir\*.bas instead of searching
all files from the root down with \*.*.
DISK FILES THIS PROGRAM USES:
^FGREP.EXE