Some uses of the Linux grep command

Source: Internet
Author: User
Tags alphabetic character expression engine egrep

Linux grep command

The Grep,egrep 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.

1.grep: The oldest text-matching program that uses the POSIX-defined basic regular expression (BRE) to match text.
2.egrep: Extended grep, which uses an extended formal expression (ERE) to match text.
3.fgrep: Fast grep, this version matches a fixed string rather than a regular expression. And is the only version that can match multiple strings in parallel.

grep syntax Format:
grep [option] ... ' PATTERN ' FILE ...

Options
-E: Use an extended regular expression to match, grep-e, or replace the Egrep command.
-F: Use a fixed string to match, grep-f or replace the traditional fgrep command.

-A #: Displays all subsequent lines that match the string.
-B #: Displays all preceding lines that match the string.
-C #: Displays the lines following the preceding line of the matching string.
-e: Typically the first non-option parameter is considered a pattern to match, or multiple schemas can be provided at the same time, as long as they are placed in single quotation marks and separated by newline characters.
When the pattern starts with a minus sign, the-e option indicates that the parameter after it is a pattern, even if he starts with a minus sign, to prevent confusion of it as an option.
-F: From the Pat-file file read mode as a match.
-I: Ignore case differences when pattern matching.
-L: Lists the file name of the matching pattern instead of printing the matching rows.
-Q: Silently, if the match succeeds, the matching row is not output to standard output; otherwise it is unsuccessful.
-S: Error message is not displayed, usually with-Q.
-V: Take the inverse.
-O: Displays only the matched string, not the line where the string is located.
--color=auto: The matching character appears in color.
-N: Displays matching lines and line numbers.
-C outputs only the count of matching rows.
Description: You can find content in multiple files at the same time, when you specify multiple files, each displayed file line will have a filename plus a colon to identify which file it came from.
You can use multiple-e or-f options to create a list of patterns to find.

Regular Expressions:
is the pattern written by a class of characters, many of which do not represent their literal meaning, but rather the functions of expression control or wildcard;

Metacharacters
does not represent its literal meaning, but is used for additional functional descriptions

Regular Expressions:
Regular expression engine

Basic Regular expression: character grep with no special meaning
Extended regular Expressions: meta-characters, which have special meanings in regular expressions Egrep,grep-e
Fgrep:fast, using regular expressions is not supported

Character Matching:
.: Matches any single character
[]: matches any single character within the specified range
[0-9], [[:d Igit:]]: Numeric characters
[A-z], [[: Lower:]]: Lowercase alphabetic characters
[A-z], [[: Upper:]]: Uppercase characters
[[: Space:]]: space character
[[:p UNCT:]]: punctuation characters
[[: Alpha:]]: Alphabetic character
[[: Alnum:]]: Numeric characters

Number of matches metacharacters: the number of occurrences of the character that is specified before it
*: Any length, the characters in front of it can appear any time
Example: A*b
AAB, ABB, B,
\?: 0 or 1 times, the characters in front of it are optional
Example: A\?b
AB, B, CB
{m}: M times, the characters before it appear m times
Example: A{2}b
AB, AAB, B, Aaaab, ABB
{M,n}: At least m times, up to N times
Example: A{2,5}b
AB, B, AaB
{m,}: at least m times
{0,n}: up to n times

Location anchoring:
^: Anchor at the beginning of the line; matches the regular expression immediately thereafter, the Bre only has a special meaning at the beginning of the regular expression, and Ere has special meanings in any position
Write on the left side of the pattern

$: 行尾锚定:匹配前面的正则表达式,在字符串或者行结尾处。BRE中仅在正则表达式的结尾处有特殊的含义,ERE中在任何位置都有特殊含义    写在模式最右侧^$:  匹配空白行\   :  通常用于打开或关闭后续字符的特殊含义,如\(...\)与\{...\}[]  : 匹配方括号内的任一字符,其中可用连字符(-)指的连续字符的范围;^符号苦出现在方括号的第一个位置,则表示匹配不在列表中的任一字符,

A string that consists of consecutive characters that do not contain special characters is called a word:
\<: The first word appears on the left side of the word, \b
\<char
\>: Ending, appearing on the right side of the word, \b
Char\>

To extend the regular expression:
Character Matching:
.
[]
[^]
Number of matches:
: Any time
?: 0 or 1 times
+: at least 1 times
{m}: exact match m times
{N,m}: {n} refers to reproduce n times; {n,m} refers to reproducing N to M times
{m,}: at least m times
{0,n}: up to n times
Anchoring:
^
$
\<, \b
\>, \b
^$, ^[[:space:] "
$
Group:
()
Cited by: \1, \2, \3

Or:
A|b:a or B
Con (c|c) at
Concat or concat?
Conc or Cat

Grep-e ' PATTERN ' FILE ...
Egrep ' PATTERN ' FILE ...

notation in common Linux/unix tools

notation
PcreVi/vim grep awk sed
* * * * *

Note: Pcre commonly used in \b to denote "the beginning or end of a word", but Linux/unix tools usually use \< to match the "starting position of a word" and \> to match "end position of word", and \y in SED can match both positions simultaneously.

Some uses of the Linux 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.