Linux grep commands and Regular Expressions

Source: Internet
Author: User
Tags egrep

Today, we will give a brief analysis of the grep commands and regular expressions in Linux. No matter new users or masters with rich experience, during our Linux career, the knowledge of grep commands and regular expressions is an important part that requires us to be familiar with and be familiar.

1. Use of the grep command

First, we will understand the grep command, grep (global search regular expression and printing), fully search for regular expressions and print out rows. This is a powerful text search tool, it can use regular expressions to search for text and print matching rows. The grep commands in Linux include grep, egrep, and fgrep. Egrep is an extension of grep and supports more re metacharacters. fgrep is fast grep, which is faster to search, but does not support regular expressions.

Then, let's get familiar with the grep command, in the format of grep [options] 'pattern' FILE. Common grep options:

-V: reverse selection. Only rows that do not conform to the mode are displayed;

-O: only the strings matched by the pattern are displayed, rather than the entire row;

-I: case insensitive;

-R: recursive search;

-A #: displays the matching rows, followed by the # Rows;

-B #: displays the matching rows, followed by the first # rows;

-C #: displays the matching rows, along with the # Rows before and after the matching;

In addition, to view the matching results, you can enter "-- color = auto" after the grep command to make the results color.

Ii. Regular Expressions

As we mentioned above, we use the grep command to search for a regular expression, so let's learn what a regular expression is.

The origins of regular expressions are not in linux. The "originator" of regular expressions may be traced back to the early research by scientists on the working principles of human neural systems. Its meaning is a logical formula for string operations. It uses predefined characters and combinations of these specific characters to form a "rule string ", this "rule string" is used to express a filtering logic for strings. Given a regular expression and another string, we can achieve the following goals:

1. Whether the given string conforms to the filtering logic of the regular expression (called "match ");

2. You can use a regular expression to obtain the desired part from the string.

A regular expression is composed of some common characters and metacharacters. Metacharacters do not represent the meaning of the character, but are used for additional functional descriptions. Regular Expressions include basic regular expressions and extended regular expressions.

III. Basic Regular Expressions

^: Specifies the conforming content at the beginning of the line. The format is "^ pattern ";

$: Specifies the conforming content at the end of the anchor line. The format is "pattern $ ";

*: Match any character next to it;

.: Match any single character;

. *: Match any character of any length (Greedy mode, matching as much as possible );

R. * h (non-anchored), ^ r. * h $ (anchored)

[]: Matches any single character in the specified range;

[^]: Matches any single character out of the specified range;

[[: Space:]: white space character;

\? : Matches 0 or 1 character next to it;

\ {M, n \}: match the first character at least m times, at most n times;

\ {M ,\}: at least m times, \ {0, n \}: at most n times, 0-n times;

\ {M \}: exact match to m times;

\ <: Beginning of the anchor, format: \ <pattern;

\>: Specifies the end of a word. Format: \> pattern;

\ <Pattern>: Specifies a word;

\ (\): Group. Format: \ (pattern \);

AB any character AB: AB. * AB;

A. B any character a. B; \ (a. B \). * \ 1 (the meaning of 1 is the same content ).

Iv. Extended Regular Expressions

.: Any single character

[]: Matches any single character in the specified range;

[^]: Matches any single character out of the specified range;

*: Match any character next to it

+: Match the first character at least once

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

(): Group, supports reference \ 1, \ 2

A | B: either.

\ <: Beginning of the anchor, format: \ <pattern;

\>: Specifies the end of a word. Format: \> pattern;

^: Specifies the content that meets the conditions at the beginning of the line.

$: Specifies the Matching content at the end of the anchor row.

How is a regular expression used in linux commands? Let's take a look at several simple examples to analyze regular expressions.

5. Practice of regular expressions and grep commands

1. display the rows starting with h of the/etc/rc. d/rc. sysinit file with no size difference;

# Grep "^ [hH]"/etc/rc. d/rc. sysinit

2. display the lines ending with sh in/etc/passwd;

# Grep "sh $"/etc/passwd

3. the/etc/fstab command starts with "#" And follows two or more white spaces and then any non-white lines;

# Grep "^ # [[: space:] \ {1, \} [^ [: space:]/etc/fstab

4. lines in the/etc/rc. d/rc. local format are displayed as "any single character n any character any single character n;

# Grep "\ (. I \). * \ 1"/etc/rc. d/rc. local

5. Find the rows in the/etc/inittab that contain the "Words Starting with s and ending with d" mode;

# Grep "\ <s [a-zA-Z] * d \>"/etc/inittab

Or # grep "\ <s [^ [: space:] * d \>"/etc/inittab

6. Search for the integer between 1-255 in the ifconfig command result;

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

7. display the rows in the/var/log/secure file that contain "Failed" or "FAILED", and then count the relevant rows.

# Egrep "(Failed | FAILED)"/var/log/secure | wc-l

8. in/etc/passwd, retrieve the username of the user whose default shell is bash and whose user ID is the smallest.

# Grep "bash $"/etc/passwd | sort-n-t:-k3 | head-1 | cut-d:-f1

Shell programming in Linux-awk Programming

Shell programming in Linux -- basic usage of sed command

Shell programming in Linux -- basic use of the grep command

Shell programming in Linux-basics and extensions of Regular Expressions

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.