Home  >  Article  >  php教程  >  Detailed explanation of grep command parameters

Detailed explanation of grep command parameters

高洛峰
高洛峰Original
2016-12-13 14:35:132346browse

1. grep command parameter options

-? Simultaneously display the upper and lower lines of matching lines, such as grep -2 'pattern' filename,
-A ? Simultaneously display the lower lines of matching lines grep -A 10 'pattern' filename
-B ? At the same time, display the matching lines? Line
-c prints the number of matches
-E Extended regular egrep

-f Two files have the same line. For example, grep -f file1 file2
-F fgrep
-h does not display the file name of the matching line
-H Displays both line and file names
-i Ignores case
-l Only displays matching file names
-n Displays line numbers
-o Only displays matching characters
-r Recursive query
-v Only displays not Matched lines

2. grep regular expression metacharacter set (basic set)

^ The beginning of the anchor line such as: '^grep' matches all lines starting with grep.
$ End of anchor line For example: 'grep$' matches all lines ending with grep. ^$ is used together to represent a blank line
. Matches a non-newline character. For example: 'gr.p' matches gr followed by any character, then p.
* matches 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.
.. mark matching characters, such as 'love', love is marked as 1.
< Anchor the beginning of a word, such as: '> Anchor the end of a word, such as 'str>' matches lines containing words ending with str.
x{m}
Repeat character x, m times, for example: 'a{5}' matches a line containing 5 a's.
x{m,}
Repeat character x at least m times, for example: 'a{5,}' matches lines with at least 5 a's.
x{m,n}
Repeat the character The metacharacter extension set of 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 g(la|oo)d matches glad or good
()+ multiple repeated grouping matches, such as A(xy)+B matches the beginning of A and the end of B More than one xy such as AxyB.AxyxyB.AxyxyxyB
x{m},x{m,},x{m,n}
has the same effect as x{m},x{m,},x{m,n}

4. POSIX character classes


[:alnum:] 0-9, A-Z, a-z

[:alpha:] A-Z, a-z

[:digit:] 0-9
[:lower:] a-z
[:upper: ] A-Z

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:grep common commandsNext article:grep common commands