Linux one command per day: grep

Source: Internet
Author: User
Tags character classes control characters posix egrep

grep (abbreviated from globally search a Regular Expression and Print)

is a powerful text search tool that can search for text using specific pattern matching (including regular expressions) and output matching rows by default. The grep family of Unix includes grep, Egrep, and Fgrep. Similar command findstr under Windows System.

Basic introduction Egrep and Fgrep commands are only a small difference from grep. Egrep and Fgrep are grep extensions that support more re-metacharacters, and fgrep are fixed grep or fast grep, which treat all the letters as words, meaning that the metacharacters in the regular expression returns to its own literal meaning, no longer special. Linux uses the GNU version of grep. It is more powerful and can use the Egrep and FGREP functions with the-G,-e,-f command line options. grep works in such a way that it searches for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as filenames. The results of the search are sent to the screen or specified files (redirected) without affecting the contents of the original file. grep can be used with shell scripts because grep describes the status of the search by returning a status value.Returns 0 if the template search succeeds, returns 1 if the search is unsuccessful, or 2 if the search file does not exist。 We can use these return values to do some automated text processing work. The ability to specify a string statement in the grep command is a regular expression, a method that allows the use of a specified string of certain special keyboard characters, which can be used to represent other characters or to further define how pattern matching works. For example: grep ". *hood" Grep.txt.txt. The command searches the file grep.txt for each line that contains the word with the string hood. The point in the command line indicates that Hood can have any character before it, and the asterisk refers to any character that is represented by the dot before the string (where the double quotation marks are optional, but must be enclosed when the statement contains a phrase or a space). Expression set
Match text
[email protected] ~]# cat grep.txt linuxhoodlinxuhood Shell Linux Hoodpythonpython is Goodshell are good
^ The start of the anchor line, such as: ' ^grep ' matches all lines that begin with grep.
grep grep . txt python is good
The end of the anchor line is as follows: ' grep$ ' matches all rows ending with grep.
grep grep . txt python is goodshell good
Match a non-newline character (' \ n ') with the word such as: ' GR.P ' matches the GR followed by an arbitrary character followed by P.
grep grep . txt python is goodshell good
* Match 0 or more previous characters such as: ' *grep ' (note * preceded by a space) match all 0 or more spaces followed by the line of grep, need to use Egrep or grep with the-e option. * 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\) ', Love is marked as 1; by \1 Reference. \< anchors the beginning of the word, such as: ' \<grep ' matches the line that contains the word that begins with grep. \> anchors the end of the word, such as ' grep\> ' matches the line that contains the word that ends with grep. X\{m\} repeats characters x,m times, such as: ' O\{5\} ' matches rows containing 5 O. X\{m,\} repeats a character x, at least m times, such as: ' O\{5,\} ' matches at least 5 rows of O. X\{m,n\} repeats the character x, at least m times, not more than n times, such as: ' O\{5,10\} ' matches rows 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. \b Word lock, such as: ' \bgrep\b ' only matches grep. The meta-character extension set used for Egrep and grep-e \+ matches one or more previous characters. such as: ' [a-z]\+able ', matching one or more lowercase letters followed by able strings, such as loveable,enable,disable, etc. \ match 0 or one of the previous characters. such as: ' Gr\?p ' matches the GR followed by one or no characters, then the line of P. A\|b\|c matches A or B or C. such as: grep|sed matching grep or sedposix character class to maintain one to the character encoding in different countries, POSIX (the portable Operating System Interface) adds special character classes such as [: Alnum:] is another notation for a-za-z0-9. You can place them in the [] number to be a regular expression, such as [a-za-z0-9] or [[: Alnum:]]. grep under Linux supports POSIX character classes in addition to Fgrep. [: Alnum:] literal numeric character [: Alpha:] literal character [:d igit:] Numeric character [: graph:] non-null character (non-space, control character) [: Lower:] lowercase characters [: Cntrl:] control characters [:p rint:] non-null characters (including spaces) [:p UNCT:] punctuation [: space:] All whitespace characters (new lines, spaces, tabs) [: Upper:] Uppercase characters [: xdigit:] hexadecimal digits (0-9 , a-f,a-f)   command options:-number Displays the number row up and down the matching line, such as: grep-2 pattern filename Displays the top and bottom 2 rows of the matching row. -A,--text is used to match binary data-b,--byte-offset indicates the number of the first character of the line before the line that matches the style is displayed. -C,--Count prints only the number of rows that match and does not display matching content.   -F File,--file=fileSpecifies a rule file whose contents contain one or more rule styles that let grep find the content of the file that matches the rule condition, formatted as a rule style per line-h,--no-filename the matching file name prefix is not displayed when searching multiple files. -i,--ignore-case ignores case differences. -O,--only-matching displays only the parts of the regular expression match. (Show only the part of a line matching PATTERN)-q,--quiet is deselected and returns only the exit status. 0 indicates that a matching row was found. -l,--files-with-matches prints a list of files that match the template. -l,--files-without-match prints a list of files that do not match the template. -n,--line-number prints the line number in front of the matching line. -s,--silent does not display error messages about non-existent or unreadable files. -v,--revert-match, displays only rows that do not match. -w,--word-regexp if referenced by \< and \>, the expression is searched as a single word. -R,-R,--recursive recursively reads all files under the directory, including subdirectories. Like what grep-r ' pattern ' testMatches the pattern in all files in the test and its subdirectories. -v,--version displays software version information. -a6 finds the contents of some characters, and then extends the contents of some characters by 6 lines-b6 finds the content of some character, and extends the contents of some characters by-C1, and the numbers that extend up to 1 lines on and off are directly affecting the number of extensions, and the result instance of the search row with the--symbol split:

Example 1: Finding the specified process

[Email protected] ~]#PS-ef|grep 'sshd'|grep-V'grep' #-v Remove the grep itself execution processRoot2108     1  0Oct31?xx:xx:xx/USR/SBIN/SSHD-Droot6544  2108  0 Geneva: -?xx:xx:xxsshd: [Email protected]/4Root14208  2108  0Nov29?xx:xx:Genevasshd: [Email protected]/2Root26721  2108  0Nov30?xx:xx:xxsshd: [Email protected]/3

Example 2: Find the specified number of processes

PS grep ' sshd ' grep ' grep ' -c4

Example 3: Search by reading keywords from a file

Catcat  rege.txt L. *catgrep -f rege.txt hnlinuxubuntu linuxredhatlinuxmint

Description

The output Test.txt file contains the content lines of the keywords read out from the Rege.txt file

Example 3: Finding keywords from a file

Catgrep'l.*x'grep'l.*x '  1: Hnlinux4: Ubuntu Linux7: LinuxMint

Example 4: Find keywords from multiple files

[Email protected] ~]#Cattest.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatredhatlinuxmint[[email protected]~]#Cattest1.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatredhatlinuxmint[[email protected]~]#grep-N'l.*x'test.txt test1.txt test.txt:1: hnlinuxtest.txt:4: Ubuntu linuxtest.txt:7: linuxminttest1.txt:1: hnlinuxtest1.txt:4: Ubuntu linuxtest1.txt:7: LinuxMint

Example 5: Displays all lines in a file ending in. txt in the current directory that contain at least 7 consecutive lowercase characters for each string

[Email protected] ~]#grep-ne'[A-z]{7,}'*. txtgreptxt1: Linuxhoodgreptxt2: Linxuhood Shellgreptxt3: Linux hoodpythongtest.txt:1: loversrsgtest.txt:6: mariadbgtest.txt:9: loverssno_rege.txt:1: loversstest1.txt:1: hnlinuxtest1.txt:2:p eida.cnblogs.comtest1.txt:7: linuxminttest.txt:1: hnlinuxtest.txt:2:p eida.cnblogs.comtest.txt:7: LinuxMint

  

Linux one command per day: grep

Related Article

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.