grep Regular Expression

Source: Internet
Author: User
Tags control characters grep regular expression printable characters expression engine egrep

Text Processing Tools

grep function: The text Search tool, according to the user-specified "mode" to match the target text line by row to check; print matching lines

REGEXP: A pattern written by a class of special characters and text characters, in which some characters (metacharacters) do not represent literal meanings, but are functions that represent control or a wildcard


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

-V: Displays rows that are not matched by pattern

-I: Ignore character case

-N: Show matching line numbers

-C: Count the number of matching rows

-O: show only the matching string

-Q: silent mode, does not output any information

-A #: after, followed by # lines

-B #: Before, front # line

-C #:context, front and back # lines

-E: Implementing a logical or relationship between multiple options # #grep –e ' cat '-e ' dog ' file

-W: Entire line matches whole word

-E: Use ERE

-F: equivalent to Fgrep, does not support regular expressions

Character Matching :

. match any single character;

[] matches any single character within the specified range

[^] matches any single character outside the specified range

brackets are added to the front, Egrep don't need

[: Alnum:] Letters and Numbers

[: Alpha:] represents any English uppercase and lowercase characters, i.e. A-z,a-z

[: Lower:] Lowercase Letters

[: Upper:] Uppercase Letters

[: Blank:] white space characters (spaces and tabs)

[: Space:] horizontal and vertical whitespace characters (compared to [: Blank:] wide range of inclusions)

[: Cntrl:] non-printable control characters (backspace, delete, alarm ...) )

[:d Igit:] decimal Digits [: Xdigit:] hexadecimal digits

[: Graph:] non-whitespace characters that can be printed

[:p rint:] printable characters

[:p UNCT:] Punctuation

[: Alnum:]: letters and Numbers

Number of matches: used after the number of characters to be specified, to specify the number of occurrences of the preceding character

* match the preceding character any time, including 0 times

Greedy mode: Match as long as possible

. * Any character of any length

\? match its preceding character 0 or 1 times

\+ matches the characters in front of it at least 1 times

\{n\} matches the preceding character n times

\{m,n\} matches the preceding character at least m times, up to n times

\{,n\} matches the preceding character up to n times

\{n,\} matches the preceding character at least n times


Group:\ (\) binds one or more characters together and treats them as a whole, such as:\ (root\) \+

The contents of the pattern in the grouping brackets are recorded in internal variables by the regular expression engine , which are named: \1, \2, \3, ...

\1 represents the character that matches the pattern between the first opening parenthesis and the matching closing parenthesis from the left

Example:\ (string1\+\ (string2\) *\)

\1 :string1\+\ (string2\) *

\2 :string2

Position anchoring: positioning where it appears

^ beginning of the line anchor, for the leftmost mode

$ Line end anchor for the right side of the pattern

^pattern$ for pattern matching entire row

^$ Empty Line

^[[:space:]]*$ Blank Line

\< or \b The first anchor for the left side of the word pattern

\> or \b ending anchor; for the right side of the word pattern

\<pattern\> Match Whole word

Egrep Extended Regular Expressions

inherit all pattern matches of grep, just no longer need \ as escape character

Egrep One more or (to use greater than sign):

A|b:a or b

C|cat:c or cat

(c|c) At:cat or Cat


1. Display the lines in the/proc/meminfo file that begin with a non-differentiated s;

Grep-i "^s"/proc/meminfo

Egrep "^ (s|s)"/proc/meminfo

2. Display the line ending with Nologin in/etc/passwd;

grep "/sbin/nologin$"/etc/passwd

3, display/etc/inittab in the beginning of #, and followed by one or more white space characters, followed by any character line;

grep "^#[[:blank:]]\{1,\}.*"/etc/inittab

4, display/etc/inittab contains: A number: (that is, two colons in the middle of a number) line;

grep--color "\:[[:d igit:]]\:"/etc/inittab

5. Display lines that start with one or more whitespace characters in the/boot/grub/grub.conf file;

grep "^[[:blank:]]"/boot/grub/grub.conf

6. Display a line in the/etc/inittab file that begins with a number and ends with a number that is the same as the beginning number;

grep "^\ ([[:d igit:]]\). *\1$"/etc/inittab

7, find out a file, 1 digits, or 2-digit number;

Egrep "\<[[:d igit:]]{1,2}\>"/etc/passwd--color

8. Display three user root, Bin, ftp uid and default shell

Egrep "^ (root|bin|ftp)"/etc/passwd |cut-d:-f1,3,7

9. Find the beginning of a word in the/etc/rc.d/init.d/functions file

grep "^\<[[:alpha:]]"/etc/init.d/functions

10. Display all IPV4 addresses in ifconfig command results

Ifconfig |egrep-o "([[:d igit:]]{1,3}.) {3} [[:d Igit:]] {1,3} "

11. Remove the base name of the/etc/init.d/network

Ls/etc/init.d/network |egrep-o "[^/]+$

12, take out the path of/etc/init.d/network

Ls/etc/init.d/network |egrep-o ". * *"

grep 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: 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.