Grep, egrep, and regular expressions

Source: Internet
Author: User
Tags expression engine egrep

Grep, egrep, and regular expressions

All files in Linux: text files are critical and commonly used for text file search.

Linux text search tools: grep, egrep, fgrep, and text search are used to search text files.

File name search is a bash feature in linux. globbing and file name configuration are similar in windows, but powerful. It is generally used with the ls command. See the following video: Basic bash features (01)

Globbing: metacharacters in: *: Used to match any length of any character ,? : Used to match a single character. []: Used to match characters within the specified range. [^]: used to retrieve characters within the inverse range.

Summary: globbing file name configuration: it not only limits the length, but also limits the available character range.

To search, you must have filtering conditions. Here, you must use regular expressions, which are very basic and important for linux text processing tools.

The metacharacters in a regular expression are combined with normal characters.

Basic Regular Expression: grep

Extended Regular Expressions: egrep, grep E,

Fgrep: fast. Regular Expressions are not supported.

Grep: Global search Regular expression and print out the line

Grep command:

Grep [OPTION] PATTEN [FILE…]

-- Color = auto

-O: only the content matched by the filter mode is displayed.

-I: the search is case-insensitive. grep "no"

-V: displays rows that cannot be matched by the filter mode.

Here we use a grep parameter -- color, which has three values: never, always, and auto. The difference between always and auto is that always will add a color tag to the matching field in all circumstances. when it passes through the pipeline or redirection, there will be more control characters and the result will become
Export ^ [[1; 32m ^ [[KGREP ^ [m ^ [[K_OPTIONS = '-- color = always'
Export ^ [[1; 32m ^ [[KGREP ^ [m ^ [[K_COLOR = '1; 32'
Auto adds color only when it is output to the terminal.
You can add
Export GREP_OPTIONS = '-- color = auto'
To achieve highlight matching, the specific color, you can use
Export GREP_COLOR = 'a; B '# The default value is 1; 31, which is highlighted in red.
, Where:
A: You can select [,] B: You can select:

0 close all properties 30 black
1 set high brightness 31 red

4 underline 32 green

5 flashes 33 yellow

7 reverse display 34 blue

8 blanking 35 purple

36 cyan

37 white

Basic Regular Expression metacharacters:

Character match:

.: With any single character

[]: Any single character in the specified range

[^]: Matches any single character out of the specified range, which is equivalent to the inverse [].

[0-9], [[: digit:], [^ 0-9] [^ [: digit:]

[A-z], [[: lower:]

[A-Z], [[: upper:]

[[: Space:] space character

[[: Punct:] punctuation marks

[0-9a-z A-Z], [[: alnum:]

[A-zA-Z], [[: alpha:]

Number of times matching: A control letter is provided after the expected matching character to express the number of times specified to match the character before it

*: Used to match any length of the character on the left, indicating 0 times, once or multiple times

"AB * c" = ac, or, abbc, or abbbc, or abbbbc

. *: Any characters in length. The default mode is greedy,

\? : Match the first character. If it matches 0 or 1 times, the character on the Left can be unavailable.

"AB \? C "= ac, or abc

\ +: Match the first (left) character once or multiple times

"AB \ + c" = abc, abbc, abbbc

\ {M \}: precisely matches the m characters on the left. AB \ {2 \} c = abbc

\ {M, n \}: At least m times, at most n times. AB \ {0, 3 \} c = ac, abc, abbc, abbbc

\ {0, 3 \}: up to 3 times

\ {M, \}: Minimum m times

Characters with fixed positions:

^: Specifies the beginning of the line. ^ AB

$: Specifies the end of the line. AB $

"^ AB. * ba $": any character starting with AB and ending with AB

"^ $": Matches blank rows. grep "^ $"/etc/rc. d | wc l indicates counting the number of blank rows.

"^ PATTERN $: Used to match the entire row. "^ A4 * $"

Word anchoring: a continuous string consisting of non-special characters

\ <: Specifies the beginning of a word, or \ B # grep "\ <AB"/etc

\>: Specifies the end of a word, or \ B # grep "AB \>"/etc

\ <PATTERN \>: matches the entire word that PATTERN can match.

# Ifconfig | grep "\ <[0-9] \ {2 \} \>": exact match of 2 digit words

GROUP :\(\)

The pattern matching content in the parentheses of the Group will be recorded by the Regular Expression Engine During execution and stored in the built-in variables; these variables are \ 1, \ 2 ,...

\ 1: Starting from the left, the first left brace, and the matching content of the pattern in the right brace. \ 2 and so on.

# "\ (AB \) * c"

Metacharacters of the extended regular expression:

Character matching: Same as the basic Regular Expression

Matching times:

*: 0 or multiple times

? : 0 or 1 time;

+: More than 1 time;

{M}: exact match m times;

{M, n}: At least m times, at most n times;

Anchored:

^: Pin the beginning of a line

$: End of the anchor row

\ <, \ B: the beginning of the word; used to represent the left side of the word pattern;

\>, \ B: The end of the word; used to represent the right of the word pattern;

GROUP:

Same as the basic Regular Expression

()

Concepts supported or

A | B: Match a or B

Exercise:

1. display the lines ending with bash in the/etc/passwd file

# Grep "bash $"/etc/passwd

# Grep-E "bash $"/etc/passwd

# Egrep "bash $"/etc/passwd

2. display two or three digits in the/etc/passwd file

# Grep "\ <[: digit:] \ {2, 3 \} \>"/etc/passwd

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

3. display the lines that end with 'listen' followed by 0, 1, or multiple white spaces in the 'netstat-tan' command results

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

# Netstat-tan | egrep "LISTEN [[: space:] * $"

4. Add bash, testbash, basher, and nologin users (the shell of nologin users is/sbin/nologin), and find the rows with the same username as the shell name in the/etc/passwd file.

# Grep "^ \ ([[: alnum:] \ + \> \). * \ 1 $"/etc/passwd

# Egrep "^ ([[: alnum:] + \>). * \ 1 $"/etc/passwd

5. display the default shell and UID of the root, CentOS, or user1 users on the current system (Please create these users in advance if they do not exist)

# Grep-E "^ (root | centos | user1) \>"/etc/passwd | cut-d:-f3, 7

# Egrep "^ (root | centos | user1) \>"/etc/passwd | cut-d:-f3, 7

6. Find a word in the/etc/rc. d/init. d/functions file (the word can contain underscores in the middle) followed by a group of parentheses.

# Egrep-o "\ <[[: alnum:] _] + \> \ (\)"/etc/rc. d/init. d/functions

7. Use echo to output a path, and then find the base name of the path for egrep. Use egrep to retrieve the directory name.

# Echo/etc/sysconfig/network-scripts/| egrep-o "[[: alnum:]-] + /? $"

# Echo/etc/sysconfig/network-scripts | egrep-o "(/.*/)"

8. Find the number between 1-In the ifconfig command execution result.

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

Linux basics tutorial ---- Regular Expression Basics

Linux Regular Expression sed details

Linux regular expression features and differences between BRE and ERE

Grep uses concise and Regular Expressions

Regular Expression usage

Assertion with Zero Width of a regular expression

Regular Expressions and file formatting commands in Linux (awk/grep/sed)

Basic Regular Expression

Regular Expressions

This article permanently updates the link address:

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.