grep, Egrep detailed

Source: Internet
Author: User
Tags expression engine egrep

grep, egrep search Tool

Do you know how to retrieve the data you need in the text?

Yes, the GREP,EGREP command can do more than that, let me introduce you.


What is grep and Egrep ?

grep, Egrep are text search tools


what can grep and Egrep do ?

1) match the target text line by row according to the user-specified pattern (filter condition);

2) Print out the qualifying line.


You must be thinking about Grep,egrep.

Egrep is a grep command that supports the use of extended regular expressions. Equivalent to GREP-E


Introduction to the grep command usage:

grep [Options] (Note: options) PATTERN (Note: Filter condition) [FILE ...] (retrieved text file)

Option 1:

--color=auto: Highlight a match to a text after coloring
-I. ignores character case;
-O Show only the text that matches to itself;
-V --invert-match: Reverse matching;
-E Support for extended regular expressions;
-Q,--quiet,--silient Silent mode, do not output any information;


Introduction to the original character of the basic regular expression ( Note: Our filter conditions are written by text characters and regular expression metacharacters.) )

1. Character Matching class:

    • .: matches any single character;

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

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

    • Character Set [:d igit:] All numbers, [: lower:] All lowercase letters, [: Upper:] All uppercase letters, [: Alpha:] All alphabets, [: alnum:] All letters and numbers, [: space:] All white space characters, [: blank:] Blank, [ :p UNCT:] All punctuation


2. Match Number class:

Use the number of occurrences of the character that you want to specify the number of occurrences, and the default work in greedy mode.

    • *: matches the preceding character any time (0,1 or more);

For example: grep "X*y" A2 Note: As long as the containing Y can match to

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/7C/F9/wKioL1bdkQmBzQj6AAAMfMuhdrk098.png " Title= "3.png" alt= "Wkiol1bdkqmbzqj6aaamfmuhdrk098.png"/>

    • . *: Any character of any length;

For example: grep "^x.*y" A2 Note:. * indicates that characters that follow any character at the beginning of x and that contain y will be matched.

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/7C/F9/wKioL1bdk_iQBu6wAAAI_CuDLQ8027.png " Title= "5.png" alt= "Wkiol1bdk_iqbu6waaai_cudlq8027.png"/>

    • \+: Matches the preceding character at least 1 times;

For example: grep "^[[:space:]]\+[[:space:]]"/etc/grub2.cfg Note: The blank line at the beginning of the \+ match appears 1 or more times

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/7C/F9/wKioL1bdkjzCKh0CAAAO2bRbGU8553.png " Title= "4.png" alt= "Wkiol1bdkjzckh0caaao2brbgu8553.png"/>

    • \?: matches the preceding 0 or 1 times, that is, the preceding character is optional;

For example: grep "X\?y" Note: As long as it contains Y, it can be matched to

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/7C/FB/wKiom1bdlV3xZg_ 4aaakgcm94jk011.png "title=" 6.png "alt=" Wkiom1bdlv3xzg_4aaakgcm94jk011.png "/>

    • \{m\}: The preceding character appears m times and M is a nonnegative integer;

      For example: grep "X\{2\}y" Note: \{2\} matches x to occur two times. Only XXXYABC meets

      650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/7C/F9/wKioL1bdlqbDlHdwAAALsnMlwyk560.png " Title= "7.png" alt= "Wkiol1bdlqbdlhdwaaalsnmlwyk560.png"/>

    • \{m,n\}: Matches the preceding character at least m times up to N times

For example: grep "\<[0-9]\{2\3}\>"/etc/passwd Note: matches at least 2 digits in the file to a maximum of 3 digits.

    • \<: Muding \>: Muding suffix, which is used to limit the matching of complete words.

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

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


3. Position anchoring class:

Restricts the use of pattern search text, which restricts the text that the pattern matches to where it appears only in the target text;

    • ^: Anchor at the beginning of the line; for the leftmost side of the pattern, for example: grep "^s" A1 Note: lines starting with S

    • $: End-of-line anchoring; for the rightmost side of the pattern, pattern$ for example: grep "s" A1 Note: lines ending in S

^pattern$: To make PATTERN match exactly a whole line; For example: grep "^xy$" A2 Note: only XY lines

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/7C/F9/wKioL1bdmd-wbSRlAAAHyQVfeIE937.png " Title= "9.png" alt= "Wkiol1bdmd-wbsrlaaahyqvfeie937.png"/>

    • ^$: blank line; For example: grep "^$" A2 Note: Find empty lines in A2

    • ^[[:space:]]*$: Blank lines or lines that contain white space characters, such as grep "^[[:space:]]*$" A2

    • \< or \b: The initial anchor for the left side of the word pattern, formatted as \<pattern, \bpattern

    • \> or \b: The ending anchor for the right side of the word pattern, formatted as PATTERN\> pattern\b

    • \<PATTERN\>: Word anchoring; Word: a continuous character (string) consisting of a non-special character is called a word;

4. Grouping and referencing classes:

    • \ (pattern\): The character matching this PATTERN is treated as a non-infringing whole;

    • Note: The patterns in the grouping brackets match the characters that are automatically recorded in the internal variables by the regular expression engine, which are \1, \2, \3, ...

\ (pat2\) pat3\ (pat4\ (pat5\) pat6\)

Note: \1 is a reference to the result of \ (PAT2) pattern matching. \2 is a reference to \ (Pat4\ (pat5\)

    • \ n: The string that matches the pattern between the nth opening parenthesis in the pattern and the closing parenthesis that matches it (not the pattern, but the result of the pattern match)

    • \1: The string that the pattern in the first set of parentheses matches to;

    • \2: The string that the pattern in the second set of parentheses matches to


EGERP Introduction

Egrep [OPTIONS] PATTERN [FILE ...]

Option 2

-E,--extended-regexp: supports the use of extended regular expressions

-F,--fixed-strings: Supports the use of fixed strings, does not support regular expressions, and is equivalent to Fgrep;

-G,--basic-regexp: Supports the use of basic regular expressions;

-P,--perl-regexp: supports the use of pcre regular expressions;

-E pattern,--regexp=pattern: multi-mode mechanism;

-F file,--file=file:file a text file containing a pattern for each line, the grep script;

-A NUM,--after-context=num

-B NUM,--before-context=num

-C NUM,-num,--context=num


Introduction to regular meta-characters of Egrep

Summary: the same point: the character match class and the anchoring class are the same.

Different points: the match number class and the grouping reference class, the control semantics of each character are the same, the wording on the extension of less write \. A mechanism for grouping references

Number of matches:

    • *: any time;

    • ?: 0 or 1 times;

    • +:1 or multiple times;

    • {m}: matches m times;

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

    • {0,n}: up to n times

    • {m,}: at least m times


Grouping and referencing:

    • (pattern): a grouping in which the pattern in parentheses matches to a character that is recorded in a variable inside the hermetical expression engine;

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

For example: or: A|b:a or b c|cat: Indicates a C or cat (c|c) at: Indicates a cat or cat


&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&

Practice Promotion

1. Display the line in the/etc/passwd file that does not end with bash;

~]# egrep-v "bash$"/etc/passwd


2. Find out the three-bit or four-digit number in the/etc/passwd file;

~]# egrep "\<[0-9]{3,4}\>"/etc/passwd


3. Find the/etc/grub2.cfg file, start with at least one whitespace character, followed by a line with non-whitespace characters;

Grep-e "^[[:space:]]+[[:space:]"/etc/grub.cfg


4. To find the result of "Netstat-tan" command, the line ending with ' LISTEN ' followed by 0 or more whitespace characters;

Netstat-tan | Grep-e "listen[[:space:]]*$"


5. Find the result of "fdisk-l" command, including the line with/dev/followed by SD or HD and a small letter;

fdisk-l | egrep "/dev/(SD|HD) [[: Lower:]]"


6. Find the file path in the result of "ldd/usr/bin/cat" command;

ldd/usr/bin/cat | grep-e "[/]+.*"


7. Find all the lines in the/proc/meminfo file that begin with uppercase or lowercase s; at least three ways;

~]# egrep "^ (s| S) "/tmp/meminfo

~]# grep "^[ss]"/tmp/meminfo

~]# grep-i "^s"/tmp/meminfo


8. Displays information about root, CentOS, or Slackware users on the current system;

~]# egrep "^ (root|centos|slackware) \>"/etc/passwd


9, echo output an absolute path, using Egrep to remove its base name;

~]# echo/etc/passwd/| Egrep-o "[^/]+/?$"


10. Find the integer between 1-255 in the result of ifconfig command;

~]# Ifconfig | Egrep "\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "


11, add user Bash, Testbash, basher and Nologin, the default shell for the first three users is/bin/bash,nologin default shell is/sbin/nologin, Then find the user whose username is the same as the shell name;

~]# egrep "^ ([[a-z0-9]+) \>.*\1$"/etc/passwd


This article is from the "Learning and Thinking" blog, please be sure to keep this source http://7136230.blog.51cto.com/7126230/1748938

grep, Egrep detailed

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.