grep Common Search

Source: Internet
Author: User
Tags grep regular expression

[[email protected] ~]# #grep [-A] [-b] [--color=auto] ' search string ' filename

Option no parameter
-A: The following can be added to the number, for after the meaning, in addition to listing the row, the subsequent n rows are also listed;
-B: The following can be added as a number, for befer meaning, in addition to listing the row, the preceding n rows are also listed
--color=auto can list the correct data color, you can try its magical effect

-C calculates the number of rows that are found to match the      -l query for multiple files only outputs the file name with matching characters in the package. 
-S does not display error messages that do not exist or have no matching text. The grep command plus the-e parameter, which allows extended pattern matching to be used.
Example :
[[email protected] lianxi]# cat sshd_config |egrep-v "^#|^$"     #从sshd_config查找不以 # and $ start line, same [email Protected] lianxi]# cat sshd_config |grep-e-V "^#|^$"
grep regular Expression meta-character set (base set)
The start sign of the line, such as: ' ^grep ' matches all lines that begin with grep. The end flag for the $ line, such as: ' grep$ ' matches all lines ending with grep. Match a non-newline character such as: ' GR.P ' matches gr followed by an arbitrary character followed by P. * Match 0 or more previous characters such as: ' *grep ' matches all of one or more spaces followed by the grep line. * Together with any character represented. [] matches a specified range of characters, 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 letter that does not contain a-f and h-z, immediately following the line of the Rep. /(.. /) Mark matching characters, such as '/(love/) ' "x\{m\}" repeat characters x,m times, such as: ' O\{5\} ' matches rows containing 5 O. X\{m,\} repeats the character x, at least m times, such as: ' O\{5,\} ' matches at least 5 O of the line "x\{m,n\}" repeats the character X, at least m times, not more than n times, such as: ' O\{5,10\} ' matches the row of 5--10 O. /w matches literal and numeric characters, that is, [a-za-z0-9_], such as: ' G/w*p ' matches with a G followed by 0 or more literal or numeric characters, followed by P. /w/w the inverse of the form, matching one or more non-word characters, such as the dot period and so on. The/b word lock, such as: '/bgrep/b ' only matches grep.
 grep-c "test.txt counts all lines that start with the" 48 "character Grep-i" may "test.txt case-insensitive find" can "  All rows) grep-n "test.txt" displays line numbers; matches the line and line number of the match character "48", same as NL test.txt |grep) grep-v "" "Test.txt show output no characters" 48 "all rows) grep" 471 " Test.txt displays the line where the output character "471" is located) grep "Test.txt" The output begins with the character "48", and after the character "48" is a tab where the line grep "48[34]" test.txt display output begins with the character "48". The third character is "3" or "4" for All rows) grep "[Mm]ay" Test.txt set case lookup: Display output the first character begins with "M" or "M", and the line that ends with the character "Ay" grep "K ... D "test.txt display output the first character is" K ", combined second is any character, the fifth character is" D "in the Row) grep" [a-z][9]d "test.txt display output the first character range is" A-Z ", the second character is" 9 ", the third character is" D "All rows of grep" [35]. 1998 "Test.txt show the first character is 3 or 5, the 23rd character is any, with 1998 end of all rows grep" 4\{2,\} "test.txt pattern appears a chance to find: Display output character" 4 "at least two times repeat all rows grep" 9\{3,\} " Test.txt the chance to find: Display output character "9" at least three times the repetition of all rows grep "9\{2,3\}" test.txt mode chance to find: Display the output character "9" repeated occurrences of the number of times in a certain range, repeating 2 or 3 times all rows Grep-n "^$" test.txt displays the line number of the output blank line ls-l |grep "^d" If you want to query directories in the directory list with: ls-d *ls-l |grep "^d[d]" in a directory to query all files that do not contain directories ls-l |grpe "^d.....x. X "Queries other users and user group members for a catalog collection that has executable permissions  
-----------------------the magic of "grep" requires a lot of discussion, too-----------------------------

[: Alnum:] represents English uppercase and lowercase characters and numbers, also 0-9, A-Z, A-Z

[: Alpha:] represents any English uppercase and lowercase characters, also A-Z, A-Z

[: Blank:] On behalf of the Cheongju grid key not [Tab] button both

[: Cntrl:] represents the keyboard above the control button, also Shaw including CR, LF, Tab, Del. Wait a minute

[:d Igit:] Represents a number, also 0-9

[: Graph:] except for whitespace (Cheongju key not [Tab] button) all other keys

[: Lower:] represents lowercase characters, also a-Z

[:p rint:] represents any word that can be printed out

[:p UNCT:] represents punctuation (punctuation symbol), also: "'?!; : # $...

[: Upper:] On behalf of uppercase characters, also a-Z

[: space:] Any character that generates whitespace, including SPACEBAR, [Tab], CR, etc.

[: Xdigit:] stands for 16 binary numeric type, so includes: 0-9, a-f, a-f digit no character

Note the focus: [: Alnum:], [: Alpha:], [: Upper:], [: Lower:], [:d igit:]

Example: [[email protected] ~]# grep-n ' [^[:lower:]]oo ' test.txt

# that [: Lower:] Represents a A-Z meaning, to [a-z]oo the beginning of the letter to find, parallel travel number

[[email protected] ~]# grep-n ' [[:d igit:]] ' test.txt

#[]:d igit:] stands for the meaning of 0-9.

grep Common Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.