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.
Suppose 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
Or ignore the subdirectory: grep-D skip
Of course, if you expect a lot of output, you can use the pipeline to transfer it to the 'less'. 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 untilProgramInterrupted. If this happens, press and try again.
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: matches only the entire word, not a part of the string. (For example, match 'Magic 'instead of '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.
5. grep Command Options
-?
At the same time, the upper and lower lines of matching rows are displayed? Line. For example, grep-2 pattern filename simultaneously displays the upper and lower rows of matching rows.
-B, -- byte-offset
Print the block number of the row before the matching row.
-C, -- count
Only the number of matched rows is printed, and the matching content is not displayed.
-F file, -- file = File
Extract templates from files. The empty file contains 0 templates, so nothing matches.
-H, -- no-filename
When multiple files are searched, the matching file name prefix is not displayed.
-I, -- ignore-case
Ignore case differences.
-Q, -- quiet
Cancel display. Only the exit status is returned. 0 indicates that the matched row is found.
-L, -- files-with-matches
Print the list of files matching the template.
-L, -- files-without-match
Print the list of files that do not match the template.
-N, -- line-Number
Print the row number before the matched row.
-S, -- silent
The error message about the nonexistent or unreadable file is not displayed.
-V, -- revert-match
Reverse search: Only unmatched rows are displayed.
-W, -- word-Regexp
If it is referenced by/<and/>, the expression is used as a word search.
-V, -- version
Displays the software version information.
6. Instance
To make good use of the grep tool, we need to write a regular expression. Therefore, we will not explain all the functions of grep here. We will only list a few examples to illustrate how to write a regular expression.
$ LS-L | grep '^'
Filter the LS-L output content in the MPs queue and display only the rows starting with.
$ Grep 'test' D *
Display all the lines containing test in files starting with D.
$ Grep 'test' AA BB CC
The row Matching Test is displayed in the AA, BB, and CC files.
$ Grep '[A-Z]/{5/} 'aa
Display All rows of a string that contains at least five consecutive lowercase characters.
$ Grep 'W/(ES/) T. */1 'aa
If West is matched, ES is stored in the memory, marked as 1, and any characters (. *). These characters are followed by another ES (/1). If they are found, the row is displayed. If you use egrep or grep-E, you do not need to escape the "/" number and write it as 'W (ES) T. */1.