Linux Base command (i) grep command

Source: Internet
Author: User
Tags character classes control characters posix regular expression

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

grep works like this by searching 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 file names. The results of the search are sent to standard output without affecting the contents of the original file.

grep is available for shell scripts because grep indicates the state of the search by returning a status value, returns 0 if the template search succeeds, or 1 if the search is unsuccessful, and returns 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:

Specific characters for filtering/searching. Regular expressions can be used in conjunction with multiple commands and are very flexible to use.

3. Command parameters:

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

-a< shows the number of columns >--after-context=< shows the number of columns > #除了显示符合范 the column of this style, and displays the contents of the column.

-B--byte-offset #在显示符合样式的那一列之前, indicating the number of the first character in the column.

-b< Displays the number of columns >--before-context=< shows the number of columns > #除了显示符 the one that is in the style, and displays the contents of the column before it.

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

-c< Displays the number of columns >--context=< the number of columns > or-< display the number of columns > #除了显示符合样式的那一列之, and displays the contents of the column before it.

-D < action >--directories=< action > #当指定 You must use this parameter when you are looking for a directory other than a file, otherwise the grep instruction returns information and stops the action.

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

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

-f< rule File >--file=< rule File > # Specifies a rule file that contains one or more rule styles, allowing grep to find the contents of the file that match the rule criteria, in a rule style for each column.

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

-G--basic-regexp #将样式视为普通 notation to use.

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

-H--with-filename #在显示符合样式的那一列之前 representing the name of the file to which the column belongs.

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

-L--file-with-matches #列出文件内 the name of the file that fits the specified style.

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

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

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

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

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

-V--revert-match #显示不包 All lines that contain matching text.

-V--version #显示版本信息.

The-W--word-regexp #只显 a column that matches the whole word.

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

The-y #此参数的效果 is the same as the specified "-i" argument.

4. Rule expression:

The rule expression for grep:

^ #锚定行的开始 such as: ' ^grep ' matches all rows beginning with grep.

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

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

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

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

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

[^] #匹配一个不在指定范围内的字符, such as: ' [^a-fh-z]rep ' match does not contain the beginning of a letter a-r and T-z, followed by the rep line.

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

\< #锚定单词的开始, such as: ' \<grep ' matches a line containing a word that starts with grep.

\> #锚定单词的结束, such as ' grep\> ' to match a line containing a word that ends with grep.

X\{m\} #重复字符x, M times, such as: ' 0\{5\} ' matches rows containing 5 O.

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 5--10 o ' line.

\w #匹配文字和数字字符, that is, [a-za-z0-9], such as: ' G\w*p ' match with G followed by 0 or more literal or numeric characters, then p.

\w #\w, which matches one or more non word characters, such as the dot number period.

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

POSIX characters:

POSIX (The Portable operating System Interface) adds special character classes, such as [: Alnum:] is another notation for [a-za-z0-9], in order to keep one to the character encoding in different countries. To put them inside the [] number, you can become regular expressions, such as [a-za-z0-9] or [[: Alnum:]]. Under Linux, grep supports the POSIX character classes except Fgrep.

[: Alnum:] #文字数字字符

[: Alpha:] #文字字符

[:d igit:] #数字字符

[: Graph:] #非空字符 (not spaces, control characters)

[: Lower:] #小写字符

[: Cntrl:] #控制字符

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

[:p UNCT:] #标点符号

[: space:] #所有空白 characters (new lines, spaces, tabs)

[: Upper:] #大写字符

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

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.