grep egrep usage and its corresponding regular expression

Source: Internet
Author: User
Tags expression engine egrep

grep egrep usage and its corresponding regular expression


One, grep, Egrep command

In this paper, the basic parameters and the usage format and methods of grep egrep fgrep command and regular expression in Linux system are mainly introduced.

Definition of 1.grep:

grep (Global search regular RE) and print out of the line, full search of regular expressions and print out the lines, is a powerful text search tool that can only use basic regular expressions to search for text and print matching lines.

grep is a common and commonly used command, and its main function is to compare string data and then print out strings that match the user's needs, but the idea is that when grep looks for a string in the data, the data is filtered in the "Whole row" unit.

The role of the 2.GREP command:

A text search tool that matches the target text line by row according to the user-specified pattern (filter), prints out qualifying rows, patterns: Filter conditions written by text characters and regular expression metacharacters.

grep is the search for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as filenames. The results of the search are sent to the screen without affecting the contents of the original file.

grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can use these return values to do some automated text processing work.

3.grep Command Basic usage

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

grep Common Options One:

--color=auto: Color The matching text to highlight;

-I: Ignore character case; Grep-i/etc/passwd

-O: Displays only the text that matches to itself; Grep-o ' Root '/etc/passwd

-v,--invert-match: Reverse match, that is, the line that shows no ' search string ' content;

Example: Displays a line in the/etc/passwd file that does not end with bash;

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

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

-A: Search for data in a binary file as a text file

-C: Calculate the number of times to find the ' search string ';

-N: Output line number;

-A #:after that displays the following # lines matching to the word Funa;

-B #:before, displays the line that matches the front # of the word Funa;

-c #:context, display matches to the word Funa before and after # lines;

Two common options for grep:

-E,--extended-regexp: Supports the use of extended regular expressions, which is equivalent to Egrep.

-F,--fixed-strings: Supports the use of fixed strings and does not support regular expressions, 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 displays the following # lines matching to the word Funa;

-B NUM,--before-context=num display matches to the front # line of the word Funa;

-C NUM,-num,--context=num display matches to the word Funa before and after # lines;

Definition of 4.egrep:

The Egrep command is a search file acquisition pattern that allows you to search for strings and symbols in a file, or to search for a string of multiple files, a prompt can be a single character, a string, a word, a sentence. The EGREP supports GREP commands that use extended regular expressions, equivalent to GREP-E.

The role of the 5.EGREP command:

The Linux egrep command is used to find the specified string within a file.

Egrep execution effect is similar to "GREP-E", the syntax and parameters used can refer to the GREP directive, and the difference between grep is the method of interpreting the string.

Egrep is interpreted with extended regular expression syntax, while grep is interpreted with the basic regular expression syntax, extended regular expression than basic Regular expression is more canonical.

6.egrep Command Basic usage

Format: egrep [OPTIONS] PATTERN [FILE ...]

Common options:

-C: Displays only the count of matching rows.

-E: mode specifies a pattern. This is like a simple pattern, but it is useful when the pattern starts with a-(minus sign).

-F:STRINGFILE Specifies the file that contains the string.

-H: Excludes file names when working with multiple files.

-I: Ignores the case of characters when making comparisons.

-N: Precede each line with the line number in the file.

-Q: Disables all output to standard output, regardless of the matching line. If the input line is selected, exit with 0 status.

-S: Displays only error messages. This is useful for checking the state.

-V: Displays all rows except those that match the specified pattern.

-W: Performs a word search.

-X: Displays lines that exactly match the specified pattern without other characters.

-Y: Ignores the case of characters when making comparisons

Second, the regular expression

1. Basic definition:

Regular expressions use a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern. In short, a regular expression is a method of handling strings, the processing of strings in behavioral units, and the assistance of special symbols that allow users to easily search/replace a particular string.

Regular expressions fall into two categories: basic regular expressions and extended regular expressions.

2.grep corresponding regular expressions are described in detail

2.1, the basic regular expression:

(1) meta-characters:

.: Matches any single character

Example: Finding a line that contains student and student with one character behind it

grep ' student. '/etc/passwd

(The pattern can be in single and double quotes, if you want to do variable substitution in the pattern, you must use double-cited)

[]: matches any single character in the specified range [abc],[a-z],[0-9],[a-za-z]

Example: Finding a row with numbers

grep ' [0-9] '/etc/passwd

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

Example: Find a line with no lowercase letters.

grep ' [^a-z] '/etc/inittab

[: Space:]: denotes white space characters;

[:p UNCT:]: Represents the collection of all punctuation marks;

[: Lower:]: denotes all lowercase letters;

[: Upper:]: denotes all uppercase letters;

[: Alpha:]: denotes uppercase and lowercase letters;

[:d igit:]: Indicates a number;

[: Alnum:]: denotes numbers and uppercase letters-----use format [[: Alnum:]] etc.

(2) Number of matches: used to specify the number of occurrences of the character after the number of characters to limit the occurrence of the preceding characters, the default work in greedy mode;

*: Matches its preceding character any time, 0, 1, multiple times;

For example: grep "X\+y" Abxy

. *: Matches any character of any length

Example: Finding rows that contain student

grep ' student.* '/etc/passwd

\?: matches the preceding characters 1 or 0 times, i.e. the characters in front of them are optional;

\+: Matches the preceding character 1 or more times, i.e. the character of its face must appear at least 1 times;

\{m\}: Matches the 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,\}: at least m times;

(3) Position anchoring:

^: Anchors the beginning of the line to the left of the pattern, meaning that any content that follows this character must appear at the top of the line.

$: End-of-line anchoring, which is used for the rightmost part of the pattern-any content preceding this character must appear at the end of the row. ^pattern$: Used for PATTERN to match whole line;

^$: blank line;

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

(4) Word: a continuous character consisting of non-special characters (a string) is called a word;

\< or \b: The first anchor of the word, used for the left side of the word pattern;

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

\<pattern\>: matches complete words;

(5) Grouping and quoting

\ (\): Bind one or more characters together and treat them 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 pattern from the left side, the second opening parenthesis, and the matching closing parenthesis to the character;

...

~]# Nano Lovers.txt


He loves his lover.

He likes his lover.

She likes her liker.

She loves her liker.

~]# grep "\ (L.. e\). *\1 "Lovers.txt

(1 refers to the matching result in the preceding parentheses)

(6) Back reference: The character that is matched to the pattern in the preceding grouping brackets;

Small test Sledgehammer:

1. Find the two-digit or three-digit number in the/etc/passwd file;

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


2, find the/etc/rc.d/rc.sysinit or/etc/grub2.cfg file, with at least one blank character beginning, and followed by a non-whitespace character line;

~]# grep "^[[:space:]]\+[^[:space:]"/etc/grub2.cfg


3. Find the line ending with ' LISTEN ' followed by 0, 1, or more whitespace characters in the result of the "Netstat-tan" command;

~]# Netstat-tan | grep "listen[[:space:]]*$"

2.2. Extended Regular Expressions:

The extended regular expression only makes a little change in the basic regular expression, as follows:

In the extended regular expression \ (\) = = (); \{\}=={};

+: The number of matches, matches the preceding character at least once, no upper limit;

A|b:a or B (two take one), the rest are the same.

Basic regular expression, using () {}. ? | All need to be escaped, in the extended regular expression does not need to add \, the details are as follows:

(1) The command and usage of character matching is the same as the usage of the basic regular expression.

(2) Number of matches:

*: Matches any of its preceding characters

?: matches 0 of its preceding characters or 1 of this

+: Match its preceding character at least 1 this

{M,n}: matches its preceding characters m to n times

(3) The usage of the character anchor is the same as the usage of the basic regular expression.

(4) Special characters:

| : or the meaning.

Example: Grep-e ' c|cat ' file means to find the files containing C or cat;

3.egrep the corresponding regular expression is described in detail:

Egrep: Support for grep commands using extended regular expressions, equivalent to GREP-E;

Extended Regular Expression metacharacters egrep [OPTIONS] PATTERN [FILE ...] :


3.1, the basic regular expression:

(1) Character matching:

.: Any single character

[]: Any single character within the range

[^]: Any single character outside the range

(2) 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;

(3) Position anchoring:

^: Anchor at the beginning of the line

$: End of line anchoring

\<, \b: The first anchor of the word

\>, \b: Final anchoring

(4) Grouping and quoting:

(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, ...

Or:

A|b:a or B

C|cat: Indicates C or cat

(C|C) at: Indicates Cat or cat

Small test Sledgehammer:

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

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


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

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

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

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


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

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


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

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


5. 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]) \> "


6, 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;

~] #useradd-S/bin/bash bash

~] #useradd-S/bin/bash testbash

~] #useradd-S/bin/bash basher

~] #useradd-S/sbin/nologin nologin

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




This article from the "Linux" blog, reproduced please contact the author!

grep egrep usage and its corresponding 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.