Use 'grep' to search for text files
If you want to find a string in several text files, you can use the 'grep' command. 'Grep' searches for the specified string in the text. For example, if you are searching for a file with the string 'Magic 'in the'/usr/src/Linux/documentation' directory: $ grep magic/usr/src/Linux/documentation /*
Sysrq.txt: * how do I enable the magic sysrq key? Sysrq.txt: * how do I use the magic sysrq key?
The 'sysrp.txt 'file contains this string. The sysrq function is discussed.
By default, 'grep' only searches for the current directory. If the directory contains many subdirectories, 'grep' is listed as follows:
Grep: Sound: is a directory
This may make the output of 'grep' difficult to read. There are two solutions:
Search for subdirectories: grep-R
Ignore subdirectory: grep-D skip
Of course, if you expect a lot of output, you can use the Pipeline
Switch it to 'less 'to read:
$ Grep magic/usr/src/Linux/documentation/* | less
In this way, you can read more conveniently.
Note that you must provide a file filtering method (* for searching all files *). If you forget, 'grep' will wait until the program is interrupted. If this happens, press Ctrl + C and try again. (Important !!)
The following are some interesting command line parameters:
Grep-I pattern files: searches case-insensitive. Case Sensitive by default,
Grep-l pattern files: only names of matched files are listed,
Grep-l pattern files: Lists unmatched file names,
Grep-W pattern files: match only the entire word, not a part of the string (for example, match 'Magic ', not 'magical '),
Grep-C number pattern files: the matching context displays the rows of [number,
Grep pattern1 | pattern2 files: displays the rows matching pattern1 or pattern2,
Grep pattern1 files | grep pattern2: displays rows that match both pattern1 and pattern2.
Here are some special symbols used for search:
\ <And \> respectively indicate the start and end of a word.
For example:
Grep man * matches 'Batman ', 'manic', 'Man ', etc,
Grep '\ <man' * matches 'manic 'and 'man', but not 'Batman ',
Grep '\ <man \>' only matches 'man ', not other strings such as 'Batman' or 'manic.
'^': Indicates the first row of the matched string,
'$': Indicates the end of a matched string,
If you are not familiar with command line parameters, you can try 'grep' on the GUI, such as rexgrep. This software provides and, or, not, and other syntaxes, as well as beautiful buttons. If you only need more explicit output, try fungrep.
Common grep command syntax 1. Double quotation marks and single quotation marks
When you input a string parameter in the g r e p command, it is best to enclose it with double quotation marks. For example, "m y s t r I n g ". There are two reasons for doing this: one is to prevent being misunderstood as the s h e l command, and the other is to find strings composed of multiple words, such as "jet plane ", if double quotation marks are not used, the word p l a n e will be mistaken for a file, and the query result will return the error message "the file does not exist.
When you call a variable, double quotation marks should also be used, such as: g r e p "$ m y va R" file name. If not
No results are returned.
When matching the call mode, use single quotes. [root @ mypc] # echo 'grep 123 111.txt '(# note that it is a single quotation mark)
2. Common g r e p options include:
-C only outputs the Count of matched rows.
-I is case insensitive (only applicable to single characters ).
-H: When querying multiple files, the file name is not displayed.
-L only names containing matching characters are output when multiple files are queried.
-N: the matching row and row number are displayed.
-S does not display the error message that does not exist or does not match the text.
-V: displays all rows that do not contain matched text.
3. Special-query multiple files
$ Grep "sort" *. DOC (# search for the string "s o r t" in all. d o C files in the current directory ")
$ Grep "sort it" * (# or query the word "sort it" in all files ")
All the following examples are used to query a single file.
4. Row matching
$ Grep-C "48" data. f
$4 (# g r e p returns the number 4, meaning four rows contain the string "4 8 ″.)
$ Grep "48" data. F (# display four lines of text containing the "4 8" string)
5. display the number of rows that meet the matching mode:
[Root @ mypc oid2000] # grep-N 1234 111.txt
:1234
3: 1234ab
6. Exact match
[Root @ mypc oid2000] # grep "1234 \>" 111.txt
1234
7. query empty rows. query the rows that start or end with a condition.
Use ^ and $ to query empty rows. Use the-n parameter to display the actual number of rows
[Root @ mypc oid2000] # grep-n "^ $" 111.txt (result 2: # the second line is empty)
[Root @ mypc oid2000] # grep-n "^ ABC" 111.txt (# query rows starting with ABC)
[Root @ mypc oid2000] # grep-N "ABC $" 111.txt (# query rows ending with ABC)
8. Match special characters and query characters with special meanings, such as $. '"* [] ^ | \ +? , Must be preceded by a specific character \.
[Root @ mypc oid2000] # grep "\." 111.txt (#query all rows containing "." In 111.txt)
[Root @ mypc oid2000] # grep "My \. conf" 111.txt (# query rows with the file name my. c o n f)
9. directory query
[Root @ mypc oid2000] # ls-L | grep "^ d" (# If you want to query the directories in the Directory List)
[Root @ mypc oid2000] # ls-L | grep "^ d [d]" (# query all files in a directory that do not contain the Directory)
[Root @ mypc] # ls-L | GRPE "^ d ..... X. x "(# query the directory set with executable permissions of other users and user group members)