One Linux command per day (--grep) command

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

The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep full name is global regular expression print, which represents the globally regular expression version, and its use rights are for all users.

grep works by searching for a string template in one or more files, and 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 standard output without affecting the contents of the source file.

grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can use these return values to do some automated text processing work.

1. Command format:

grep [option] Pattern file

2. Command function:

A specific character used for filtering/searching. The use of regular expressions can be used with multiple commands, the use of very flexible.

3. Command parameters:

-A--text #不要忽略二进制的数据.

-a--after-context=< displays the number of rows > #除了显示符合范本样式的那一列之外 and displays the contents after that line.

-B--byte-offset #在现实符合样式的那一行之前, identifying the number of the first character of the line.

-B Displays the number of rows--before-context=< displays the number of rows > #除了显示符合样式的那一行之外, and displays the contents before the line.

-C--count #计算符合样式的列数.

-c< Displays rows >--context=< displays rows > or-< Displays the number of rows > #除了显示符合样式的那一行之外, and displays the contents before and after the line.

-D--directories #当指定要查找的是目录而非文件时, this parameter must be used, otherwise the grep command returns information and stops the action.

-e--regexp=< template style > #指定字符串作为查找文件内容的样式.

-E--extended-regexp #将样式为延伸的普通表示法来使用.

-F--file=< Rules file > #指定规则文件, whose contents contain one or more rule styles, let grep find the contents of the file that match the rule condition, in the format of one rule style per line.

-F--fixed-regexp #将样式视为固定字符串的列表.

-G--basic-regexp #将样式视为普通的表达法来使用.

-H--no-filename #在显示符合样式的那一行之前, does not indicate the name of the file to which the line belongs.

-H--with-filename #在显示符合样式的那一行之前 that represents the name of the file to which the row belongs.

-I.--ignore-case #忽略字符大小写的差别.

-L--file-with-matches #列出文件内容符合指定的样式的文件名称.

-L--files-without-match #列出文件内容不符合指定的样式的文件名称.

-N--line-number #在显示符合样式的那一行之前, indicating the number of columns in the row.

-Q--quiet or--silent #不显示任何信息.

-R--recursive #此参数的效果和指定的 the "-D recurse" parameter.

-S--no-messages #不显示错误信息.

-V--revert-match #显示不包含匹配文本的所有行.

-V--version

-W--word-regexp #只显示全字符合的列.

-X--line-regexp #只显示全列符合的列.

-y #此参数的效果和指定 the same as the "-i" parameter.

 4. Rule expressions:

Rule Expressions for grep:

^ #锚定行的开始, such as: ' ^grep ' matches all lines that begin with grep.

$ #锚定行的结束, such as: ' grep $ ' matches all lines that end with grep.

. #匹配一个非换行符的字符, such as ' GR.P ' matches gr followed by an arbitrary character followed by P.

* #匹配零个或多个先前字符, such as ' *grep ' matches all one or more spaces followed by the line of grep.

. * #一起用代表任意字符.

[] #匹配一个指定范围内的字符, such as ' [Gg]rep ' matches grep and grep.

[^] #匹配一个不在指定范围内的字符, such as ' [^a-fh-z]rep ' matches the beginning of a letter that does not contain a-r and t-z, immediately following the line of the Rep.

\(.. \) #标记匹配字符, such as ' \ (love\) ', Love is marked as 1.

\< #锚定单词的开始, such as: ' \<grep ' matches lines that contain words that begin with grep.

\> #锚定单词的结束, such as ' grep\> ', matches lines that contain words ending with grep.

X\{m\} #重复字符x, M times, such as ' 0\{5\} ' match a row containing 5 0.

   X\{m,\} #重复字符x, at least m times, such as ' o\{5,\} ' matches rows with at least 5 O.

X\{m,n\} #重复字符x, at least m times, not more than n times, such as ' o\{5,10\} ' matches rows of 5--10 O.

\w #匹配文字和数字字符, 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 reverse form, matching one or more non-word characters, such as the dot period, and so on.

\b #单词锁定符, such as ' \bgrep\b ' only match grep.

POSIX characters:

POSIX (The Portable Operating system interface) adds special character classes to the character encodings in different countries, 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:] #文字数字字符

[: Alpha:] #文字字符

[:d igit:] #数字字符

[: Graph:] #非空字符 (non-whitespace, control characters)

[: Lower:] #小写字符

[: Cntrl:] #控制字符

[:p rint:] #非空字符 (including spaces)

[:p UNCT:] #标点符号

[: Space:] #所有空白字符 (New line, Space, TAB)

[: Upper:] #大写字符

[: Xdigit:] #十六进制数字 (0-9,A-F,A-F)

5. Usage Examples:

Example 1: Finding the specified process

Command: PS-EF | grep SVN

Example 2: Find the specified number of processes

Command: PS-EF | grep svn-c

Ps-ef | Grep-c SVN

Example 3: Search by reading keywords from a file

Command: Cat Test.txt | Grep-f Test2.txt

The output Test.txt file contains the content line of the keywords read from the test2.txt file, the-f parameter

Example 3: Read keywords from a file search and display line numbers

Command: Cat Test.txt | GREP-NF Test2.txt

The-n parameter displays the line number,

Example 4: Finding keywords from a file

Command: grep ' Linux ' test.txt

Example 5: Find keywords from multiple files

Command: grep ' Linux ' Test.txt test2.txt

Multiple files, when the output of a query to the information content line, the name of the file is the first output and ":" as the identifier

grep ' Linux ' *.txt looks for Linux characters in all txt files.

Instance 6:grep does not show itself process

Command: PS aux | grep \[s]sh

----Warning:bad syntax. It was a warning of grammatical errors.

Example 7: Find the line content starting with U

Command: Cat Test.txt | grep ^u

Example 8: Output line content that does not start with u

Command: Cat Test.txt | grep ^[^u]

Example 9: Output line content ending in hat

Command: Cat Test.txt | grep hat$

Example 10: Output IP address ifconfig eth0 | grep "[0-9]\{1,3\}\." ...] Omitted later, this can match a 192.

[0-9] Match a number within 0-9, x\{1,3\} Repeat the character x, at least 1 times, not more than 3 times.   \.  \ is a translator, which means "." In the following "." 。

Example 11: Displaying a content line containing an ED or at character

Command: Cat Test.txt | Grep-e "Ed | At "

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

Command: grep ' [a-z]\{7\} ' *.txt

One Linux command per day (--grep) command

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.