Linux grep command, regular expression

Source: Internet
Author: User
Tags posix expression engine egrep

grep command and regular expression


grep command

grep, Egrep, Fgrep


Grep,sed,awk Text Processing Three Musketeers


Grep:global search REgular expression and Print out of the line; finds the regular expression and displays the matching rows;


Regular expressions

Regular expressions are mainly applied to text, so it is applied in various text editor situations, and many programming languages support string manipulation using regular expressions;


The mainstream regular engine is divided into three categories: DFA, the traditional NFA; POSIX NFA;

Dfa:awk,egrep,flex,mysql,procmail, etc.;


Traditional type Nfa:gun emacs,java,ergp,less,. NET language, PCRE Library,perl,php,python,ruby,sed,vi,vim;


POSIX nfa:mawk,mortice Kern System ' Utilities,gun Emacs (can be explicitly specified when used);


DFA/NFA mix: GUN awk,gun grep/egrep,tcl;


grep usage:

grep: External command


Function: Displays the rows that are matched by the pattern from the source data;


Note: grep default mode of operation is greedy mode, that is, each grep pattern matching is done according to as many as possible to complete the matching criteria;


grep [OPTIONS] PATTERN [FILE ...]


grep [OPTIONS] [-e PATTERN] [-F FILE] [file]


Common options:

-A NUM,--After-context=num: The line to which the pattern is matched and the subsequent # line, if not, then does not appear; cannot be used with the-o option;

-B num,-before-context=num: The line that is matched by the pattern and its first # line, which is not displayed if not, and cannot be used in conjunction with the-o option;

-C NUM,--Context=num: The line that is matched by the pattern and the # lines before and after it, it is not displayed before or after it, and cannot be used with the-o option;

--color=auto: Coloring the text to match to the display;

-E pattern,--Regrep=pattren: Implements the matching of multiple patterns in a logical or relationship, with each-e option having only one schema as the parameter;

-E,--extended-regexp: extended Regular expression switches enable grep to use extended regular expressions with egrep;

-I,--ignore-case: ignore character case;

-N,--line-number: Displays the line that matches to and adds the line number to it (the line number in the file)

-o,--only-matching: Displays only the matched string;

-Q,--quiet--silent: Silent mode, do not output any information;

-V,--invert-match: Displays rows that are not matched to;

-w,-word-regexp: Entire line matches whole word;


Exit Status:

If the selected row is successfully found, the status return value is 0; otherwise 1;


Basic regular Expression meta-characters:


1. Character Matching:


. : matches any single character;

[]: matches any single character within the specified range;

[^]: matches any single character outside the specified range;

[:d igit:]: denotes all decimal digits equivalent to [0-9];

[: Lower:]: denotes all lowercase letters, equivalent to [A-z];

[: Upper:]: Denotes all uppercase letters, equivalent to [A-z];

[: Alpha:]: denotes all letters, including uppercase and lowercase;

[: alnum:]: means all uppercase and lowercase letters and decimal digits are included;

[: space:]: denotes all whitespace characters;

[:p unct:]: denotes all special characters;


2. Number of matches (used after the character to specify the number of occurrences):


*: Match its preceding character any time;

. *: Matches any character of any length;

\?: matches its preceding character 0 or 1 times;

\+: Matches its preceding character 1 or more times;

\{m\}: Matches its preceding character m times;

\{m,n\}: Matches its preceding character at least m times, up to n times;

\{0,n\}: Up to n times; \{m,0\}: at least m times;


3. Location anchoring:


^: anchor at the beginning of the line, for the leftmost mode;

$: line anchor; for the right-hand side of the pattern;

^$: blank line;

^[[:space:]]*$: A blank line or a line containing white space characters;

\< or \b: The first anchor of the word;

\> or \b: final anchoring;

\<pattern\>: matches complete words;


4. Grouping and referencing:


\ (\): Bind one or more characters together and treat as a whole; Example: \ (xy\) *ab

Note: The contents of the pattern matching in the grouping brackets are automatically recorded in the internal variables by the regular expression engine, and these variables are:

\ 1: The pattern from the left side, the first opening parenthesis and the matching closing parenthesis, matches the character of the pattern;

\ 2: ... The second one ....

\ 3: ...


Egrep

An extended regular expression implementation is similar to the grep text filtering feature: GREP-E


Egrep [OPTIONS] pattren [FILE ...]

Options:

-i,-o,-v,-q,-a,-b,-c

-G: Support for basic regular expressions, equivalent to grep;


Extend the metacharacters of regular expressions:

In addition to not having to use the escape character "\" in the number of matches, the other is the same as the metacharacters of the basic regular expression;


Fgrep

Regular expression metacharacters are not supported, and it is better to use fgrep when there is no need to use meta-characters to write patterns;



Application Examples:

1. Remove the base name of the/etc/sysconfig/network-scripts/ifcfg-eno16777736 file

~]# echo/etc/sysconfig/network-scripts/ifcfg-eno16777736 | Grep-o "[^\/]\+\/\?$"


2. Find the line with a parenthesis followed by a word (including an underscore) in the/etc/rc.d/init.d/functions file

~]# grep-o ". *_*.* ()"/etc/init.d/functions


3. Display user account information in the/etc/passwd file with the same user name as the user's default shell name

~]# grep-e "(\<.+\>). *\1$"/etc/passwd


4. Displays the absolute path of the dynamic library file used by the LS command that is seen with the LDD command

~]# Ldd/bin/ls | Egrep-o "/.*lib (+)/[^[:space:]]+"


5.grep can also find matching characters from multiple files, such as:

~]# grep ' ro './*


Linux grep command, regular expression

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.