Function description: Find strings that meet the conditions in the file.
Syntax: grep [-abcEFGhHilLnqrsvVwxy][-A
Supplementary Note: The grep command is used to find files containing the specified template style. file, if the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name given is "-", the grep command will read data from the standard input device.
Parameters:
-a or --text Don’t ignore binary data.
-A
-b or --byte-offset Before displaying the column that matches the template style, mark the bit number of the first character of the column.
-B
-c or --count Count the number of columns that match the template style.
-C
-d
-e or --regexp= Specifies a string as the template style for searching file content.
-E or --extended-regexp Use the template style as extended ordinary notation.
-f or --file= specifies a template file whose content contains one or more template styles, allowing grep to find file contents that meet the template conditions. The format is one template style per column. .
-F or --fixed-regexp Treat template styles as a list of fixed strings.
-G or --basic-regexp Treat the template style as a normal representation.
-h or --no-filename does not indicate the file name to which the column belongs before displaying the column that matches the template style.
-H or --with-filename Before displaying the column that conforms to the template style, indicates the file name to which the column belongs.
-i or --ignore-case Ignore the difference between upper and lower case characters.
-l or --file-with-matches Lists the file names whose content matches the specified template style.
-L or --files-without-match List the names of files whose content does not match the specified template style.
-n or --line-number Before displaying the column that matches the template style, mark the column number of the column.
-q or --quiet or --silent does not display any information.
-r or --recursive This parameter has the same effect as specifying the "-d recurse" parameter.
-s or --no-messages Do not display error messages.
-v or --revert-match Reverse the search.
-V or --version Display version information.
-w or --word-regexp Only displays columns that match the full word.
-x or --line-regexp Only display columns that match all columns.
-y This parameter has the same effect as specifying the "-i" parameter.
--help Online help.
Usage of linux grep command
Use grep command to search text files from www.linuxso.com
If you want to find a string in several text files, you can use the ‘grep’ command. ‘grep’ searches text for a specified string.
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 file ‘sysrp.txt’ contains this string and discusses the function of SysRQ.
By default, ‘grep’ only searches the current directory. If there are many subdirectories under this directory, 'grep' will list it like this:
grep: sound: Is a directory
This may make the output of 'grep' difficult to read. There are two solutions here:
Explicitly ask to search the subdirectory: grep -r
or ignore the subdirectory: grep -d skip
Of course, if a lot of output is expected, you can pipe it to 'less' Read
$ grep magic /usr/src/linux/Documentation/* | less
This way, you can read more conveniently.
One thing to note is that you must provide a file filtering method (use * to search all files). If you forget, 'grep' will wait until the program is interrupted. If you encounter this, press
Here are some interesting command line parameters:
grep -i pattern files: Search case-insensitively. The default is case-sensitive,
grep -l pattern files: only match file names are listed,
grep -L pattern files: list unmatched file names,
grep -w pattern files: only match whole words, not Part of the string (such as matching 'magic', not 'magical'),
grep -C number pattern files: matching context displays [number] lines respectively,
grep pattern1 | pattern2 files: displays lines matching pattern1 or pattern2 ,
grep pattern1 files | grep pattern2: Display lines that match both pattern1 and pattern2.
There are also some special symbols for searching:
< and > mark the beginning and end of words respectively.
For example:
grep man * will match 'Batman', 'manic', 'man', etc.,
grep '
'^': means that the matched string is at the beginning of the line,
'$': means that the matched string is at the end of the line,
If you are not used to command line parameters, you can try 'grep' in the graphical interface, such as reXgrep. This software provides syntax such as AND, OR, NOT, and beautiful buttons :-). If you just need clearer output, try fungrep .
.grep Search string
Command format:
grep string filename
There are many ways to find strings. For example, I want to find all lines starting with M. At this time, the concept of pattern must be introduced. Here are some simple ones □Example, and explanation:
^M Lines starting with M, ^ means the beginning
M$ Lines ending with M, $ means the end
^[0-9] Lines starting with a number, [] Can enumerate letters
^[124ab] Lines starting with 1, 2, 4, a, or b
^b.503 Period represents any letter
* Asterisk represents more than 0 letters (can be none)
+ Plus sign Represents more than 1 letter
. Slashes can remove special meanings
cat passwd | grep ^s Lists the list of exchange students who have applied for accounts
cat passwd | grep '^b.503' Lists all grades in the Department of Electrical Engineering...
grep '^.' myfile.txt Lists all lines starting with a period
Match a non-newline character. For example: 'gr.p' matches gr followed by any character, then p.
*
Match zero or more previous characters. For example: '*grep' matches all lines with one or more spaces followed by grep. .* used together represents any character.
[]
matches characters within a specified range, such as '[Gg]rep' matches Grep and grep.
[^]
matches a character that is not within the specified range, such as: '[^A-FH-Z]rep' matches a line starting with a letter that does not contain A-R and T-Z, followed by rep.
(..)
marks matching characters, such as '(love)', love is marked as 1.
<
Anchor the beginning of a word, like: '
>
Anchor the end of a word, like 'grep>' Matches lines that contain words ending with grep.
x{m}
Repeat the character x, m times, such as: '0{5}' matches lines containing 5 o's.
x{m,}
Repeat the character x, at least m times, such as: 'o{5,}' matches lines with at least 5 o's.
x{m,n}
Repeat the character x, at least m times and no more than n times. For example: 'o{5,10}' matches lines with 5--10 o's.
w
matches literal and numeric characters, that is, [A-Za-z0-9], for example: 'Gw*p' matches G followed by zero or more literal or numeric characters, then p. The inverted form of
W
w matches one or more non-word characters, such as period, period, etc.
b
Word lock character, such as: 'bgrepb' only matches grep.
3. Metacharacter extension set for egrep and grep -E
+
matches one or more previous characters. For example: '[a-z]+able', matches a string of one or more lowercase letters followed by able, such as loveable, enable, disable, etc.
?
Matches zero or more previous characters. For example: 'gr?p' matches lines with gr followed by one or no characters, then p.
a|b|c
matches a or b or c. For example: grep|sed matches grep or sed
()
grouping symbols, such as: love(able|rs)ov+ matches loveable or lovers, matches one or more ov.
x{m},x{m,},x{m,n}
functions the same as x{m},x{m,},x{m,n}
4. POSIX character class
for To maintain the same character encoding in different countries, POSIX (The Portable Operating System Interface) adds special character classes, such as [:alnum:] which is another way of writing A-Za-z0-9. They must be placed within [] signs to become regular expressions, such as [A- Za-z0-9] or [[:alnum:]]. Except for fgrep, grep under Linux supports POSIX character classes.
[:alnum:]
Alphanumeric characters
[:alpha:]
Literal characters
[:digit:]
Numeric characters
[:graph:]
Non-empty characters (non-space, Control characters)
[:lower:]
lowercase characters
[:cntrl:]
Control characters
[:print:]
Non-empty characters (including spaces)
[:punct:]
Punctuation
[:space:]
All whitespace characters (new lines, spaces, tabs)
[:upper:]
Uppercase characters
[:xdigit:]
Hex digits ( 0-9, a-f, A-F)
5. Grep command option
-?
Show matching lines above and below simultaneously? lines, such as: grep -2 pattern filename displays the upper and lower lines of the matching line at the same time.
-b, --byte-offset
Print the block number where the line is located before printing the matching line.
-c,--count
Only prints the number of matching lines and does not display the matching content.
-f File, --file=File
Extract template from file. An empty file contains 0 templates, so nothing matches.
-h, --no-filename
When searching for multiple files, do not show matching filename prefixes.
-i, --ignore-case
Ignore case differences.
-q, --quiet
Cancel the display and only return the exit status. 0 means a matching row was found.
-l, --files-with-matches
Print a list of files matching the template.
-L, --files-without-match
Print a list of files that do not match the template.
-n, --line-number
Print the line number in front of the matching line.
-s, --silent
Do not display error messages about non-existent or unreadable files.
-v, --revert-match
Reverse retrieval, only display unmatched lines.
-w, --word-regexp
If quoted by < and >, search the expression as a word.
-V, --version
Display software version information.
6. Examples
To use grep well, you actually need to write regular expressions well, so we will not explain all the functions of grep with examples here. We will only list a few examples to explain how to write a regular expression.
$ ls -l | grep '^a'
Filter the output of ls -l through the pipeline and only display lines starting with a.
$ grep 'test' d*
Display all lines containing test in files starting with d.
$ grep 'test' aa bb cc
Displays the lines matching test in the aa, bb, cc files.
$ grep '[a-z]{5}' aa
Displays all lines containing strings each with at least 5 consecutive lowercase characters.
$ grep 'w(es)t.*1' aa
If west is matched, es is stored in the memory and marked as 1, and then searches for any number of characters (.*). These characters are followed by another es (1). If found, the line is displayed. If you use egrep or grep -E, you don't need to escape with the "" sign, just write it directly as 'w(es)t.*1'.