📄 util.doc
字号:
An alternate form of this option lets you specify the set of legal word
characters. Its form is -w[set], where set is any valid regular
expression set definition.
If you define the set with alphabetic characters, it is automatically
defined to contain both the uppercase and lowercase values for each
letter in the set (regardless of how it is typed), even if the search
is case-sensitive. If you use the -w option in combination with the -u
option, the new set of legal characters is saved as the default set.
-z Verbose: GREP prints the file name of every file searched. Each
matching line is preceded by its line number. A count of matching lines
in each file is given, even if the count is zero. This option is off by
default.
------------------------------------------------------------------------------
- 15 -
------------------ Remember that each of GREP's options is a switch: Its
Order of state reflects the way you last set it. At any given time,
precedence each option can only be on or off. Each occurrence of a
------------------ given option on the command line overrides its previous
definition. Given this command line,
grep -r -i - -d -i -r - main( my*.c
GREP runs with the -d option on, the -i option on, and the
-r option off.
You can install your preferred default setting for each
option in GREP.COM with the -u option. For example, if you
want GREP to always do a verbose search (-z on), you can
install it with the following command:
grep -u -z
The search string ==========================================================
To use GREP well, you'll need to become proficient at
writing search strings. The value of searchstring defines
the pattern GREP searches for. A search string can be
either a regular expression or a literal string.
o In a regular expression, certain characters have special
meanings: They are operators that govern the search.
o In a literal string, there are no operators: Each
character is treated literally.
You can enclose the search string in quotation marks to
prevent spaces and tabs from being treated as delimiters.
The text matched by the search string cannot cross line
boundaries; that is, all the text necessary to match the
pattern must be on a single line.
A regular expression is either a single character or a set
of characters enclosed in brackets. A concatenation of
regular expressions is a regular expression.
- 16 -
------------------ When you use the -r option (on by default), the search
Operators in string is treated as a regular expression (not a literal
regular expression). The following characters take on special
expressions meanings:
------------------
---------------------------------------------------------------------------
Option Meaning
---------------------------------------------------------------------------
^ A circumflex at the start of the expression matches the start of a
line.
$ A dollar sign at the end of the expression matches the end of a
line.
. A period matches any character.
* An expression followed by an asterisk wildcard matches zero or more
occurrences of that expression. For example, in to*, the * operates
on the expression o; it matches t, to, too, etc. (t followed by zero
or more os), but doesn't match ta.
+ An expression followed by a plus sign matches one or more
occurrences of that expression: to+ matches to, too, etc., but not
t.
[ ] A string enclosed in brackets matches any character in that string,
but no others. If the first character in the string is a circumflex
(^), the expression matches any character except the characters in
the string.
For example, [xyz] matches x, y, or z, while [^xyz] matches a and b,
but not x, y, or z. You can specify a range of characters with two
characters separated by a hyphen (-). These can be combined to form
expressions (like [a-bd-z?], which matches the ? character and any
lowercase letter except c).
\ The backslash escape character tells GREP to search for the literal
character that follows it. For example, \. matches a period instead
of "any character." The backslash can be used to quote itself; that
is, you can use \\ to indicate a literal backslash character in a
GREP expression.
---------------------------------------------------------------------------
- 17 -
Note Four of the "special" characters ($, ., *, and +) don't
have any special meaning when used within a bracketed
set. In addition, the character ^ is only treated
specially if it immediately follows the beginning of
the set definition (immediately after the [ delimiter).
Any ordinary character not mentioned in the preceding
list matches that character. For example, the greater
than sign, >, matches the greater than sign (>), #
matches #, and so on.
File =======================================================
specifications
file(s) tells GREP which files (or groups of files) to
search. file(s) can be an explicit file name, or a
"generic" file name incorporating the DOS ? and *
wildcards. In addition, you can enter a path (drive and
directory information) as part of file(s). If you give
file(s) without a path, GREP searches the current
directory.
If you don't specify any files, input to GREP must come
from redirection (<) or a vertical bar (|).
Some GREP examples =======================================================
The following examples show how to combine GREP's
features to do different kinds of searches. They assume
GREP's default settings are unchanged.
------------------ The search string here tells GREP to search for the
Example 1 word main with no preceding lowercase letters ([^a-z]),
------------------ followed by zero or more occurrences of blank spaces
(\ *), then a left parenthesis.
Since spaces and tabs are normally considered to be
command-line delimiters, you must quote them if you
want to include them as part of a regular expression.
In this case, the space after main is quoted with the
backslash escape character. You could also accomplish
this by placing the space in double quotes.
Command line:
grep -r [^a-z]main\ *( *.c
- 18 -
Matches: main(i:integer)
main(i,j:integer)
if (main ()) halt;
if (MAIN ()) halt;
Does not match:
mymain()
Files searched:
*.C in current directory.
------------------ Because the backslash (\) and period (.) characters
Example 2 usually have special meaning in path and file names,
------------------ you must place the backslash escape character immedi-
ately in front of them if you want to search for them.
The -i option is used here, so the search is not case
sensitive.
Command line:
grep -ri [a-c]:\\data\.fil *.c *.inc
Matches: A:\data.fil
c:\Data.Fil
B:\DATA.FIL
Does not match:
d:\data.fil
a:data.fil
Files searched:
*.C and *.INC in current directory.
------------------ This format defines how to search for a given word.
Example 3
------------------ Command line:
grep -ri [^a-z]word[^a-z] *.doc
Matches: every new word must be on a new line.
MY WORD!
word--smallest unit of speech.
In the beginning there was the WORD, and
the WORD
Does not match:
Each file has at least 2000 words.
He misspells toward as toword.
- 19 -
Files searched:
*.DOC in the current directory.
------------------ This format defines another, even more basic single-
Example 4 word search.
------------------
Command line:
grep -iw word *.doc
Matches: every new word must be on a new line
However,
MY WORD!
word: smallest unit of speech which conveys
In the beginning there was the WORD, and
Does not match:
each document contains at least 2000 words!
He seems to continually misspell "toward"
as "toword."
Files searched:
*.DOC in the current directory.
------------------ This is an example of how to search for a string with
Example 5 embedded spaces.
------------------
Command line:
grep "search string with spaces" *.doc *.c
a:\work\myfile.*
Matches: This is a search string with spaces in it.
Does not match:
This search string has spaces in it.
Files searched:
*.DOC and *.C in the current directory, and
MYFILE.* in a directory called \WORK on
drive A.
- 20 -
------------------ This example searches for any one of the characters
Example 6 " . : ? ' and , at the end of a line.
------------------
The double quote within the range is preceded by an
escape character so it is treated as a normal character
instead of as the ending quote for the string. Also,
the $ character appears outside of the quoted string.
This demonstrates how regular expressions can be
concatenated to form a longer expression.
Command line:
grep -rd "[ ,.:?'\"]"$ \*.doc
Matches: He said hi to me.
Where are you going?
In anticipation of a unique situation,
Examples include the following:
"Many men smoke, but fu man chu."
Does not match:
He said "Hi" to me
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -