20150827-linux grep text Filtering tool and regular expression

Source: Internet
Author: User
Tags expression engine egrep


To be perfected .....

Grep:

Linux Text Processing Three musketeers:

grep: Text filtering tool;

Sed: Text editor (line); stream editor

awk: Text report Generator; awk implementation on Linux is gawk


Grep:global search REgular expression and Print out of the line.

Function: Text Search tool, according to user-specified "pattern" line to search for target text, print the matching line;

Pattern: The filter condition written by metacharacters and text characters of regular expressions;


Metacharacters: A character does not represent its literal meaning, but is used to denote a wildcard or control function;


Divided into two categories:

Basic Regular Expressions: BRE

Extended Regular expression: ERE


The regular expression engine;


grep [OPTIONS] PATTERN [FILE ...] :

Options:

--color=auto: The matching to the string to do highlighting;

-V: The display mode does not match the row;

-I: ignore character case;

-O: Displays only strings that can be matched to the pattern;

-Q: Silent mode;

-E: Using extended regular expressions;


The metacharacters of the basic regular expression:

Character Matching:

.: matches any single character;

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

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


[: Lower:], [: Upper:], ...


Number of occurrences: used to specify the number of times after the character;

*: any time;

Abxy

Xay

Xxxxxxxy


grep "X*y"


\?:0 or 1 times;

grep "X\?y"


\+:1 or multiple times;

\{m\}: Precisely limited to M times;

\{m,n\}: At least m times, up to n times, [M,n]

\{0,n\}: Up to n times;

\{m,\}: at least m times;


. *: Matches any character of any length;


Location anchoring:

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

$: End of line anchoring; for the rightmost side of the pattern;

\< \b: The first anchor of the word; the left side of the pattern used to denote the word;

\> \b: the ending anchor; the right side of the pattern used to represent the word;

^$: blank line;


Group: \ (\)


The contents of the pattern in the grouped parentheses are recorded by the regular expression engine during execution, and the built-in variables are saved: The variables are \1, \2, ...

\1: From the left, the first opening parenthesis, and the matching right parenthesis in the middle of the pattern match to the content;

\2:

...


Back reference: Use a variable to refer to the character that matches the pattern in the preceding grouping brackets;


Practice:

1. Display the line in the/etc/passwd file ending with bash;

2. Display the two-digit or three-digit number in the/etc/passwd file;

3, display the ' Netstat-tan ' command results with ' LISTEN ' followed by 0, one or more blank characters end of the line;

4, add user Bash,testbash, basher and Nologin user (Nologin the user's shell is/sbin/nologin), and then find the/etc/passwd file in the user name with the shell name of the line;


An extended regular expression:

The grep family has three commands:

grep: basic Regular expression

-e: extending regular expressions

-F: Regular expressions are not supported

Egrep: Extending Regular expressions

Fgrep: Regular expressions are not supported


Extend the metacharacters of regular expressions:

Character Matching:

.: Any single character

[]:

[^]:


Number of matches:

*

?: 0 or 1 times;

+: more than 1 times;

{m}: exact match m times;

{M,n}: At least m times, up to n times;


Anchoring:

^: Anchoring the beginning of the line

$: Anchor Line End

\<, \b

\>, \b


Group: ()

Back reference: \1, \2, ...


Or:

A|b

C|cat: does not represent cat or cat, but is C or cat;

To be written (C|c) at


Practice:

1. Display the default shell and UID of root, CentOS or User1 user on the current system;

2, find a word in the/etc/rc.d/init.d/functions file (the middle of the word can be underlined) followed by a set of parentheses line;

3, use echo to output a path, and then egrep find its path base name;


Further: Use Egrep to remove its directory name;


4. Find out the number between 1-255 in the result of ifconfig command execution;


Use of the grep command;

[[: Space:]]\{1,\}


This article is from "I take fleeting chaos" blog, please be sure to keep this source http://tasnrh.blog.51cto.com/4141731/1691289

20150827-linux grep text Filtering tool and 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.