grep Family and regular expressions

Source: Internet
Author: User

1.the grep family concept

1.grep Family Concept


grep : Supports the use of basic extension expressions

Egrep : Supports the use of extended regular expressions

Fgrep : Using regular expressions is not supported

function: A powerful text search tool that matches the target text line-by-row with a user-specified "Pattern(filter)" To print out qualifying rows

Pattern: Filter conditions written by metacharacters and text characters of regular expressions


2.grep command Explanation


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

Common options:--color=auto: Color-coded after matching to text-I: case insensitive to Characters-O: Only the text that matches to is displayed itself-V,--invert-match: Reverse match-e: Regular expressions that support extensions-Q,--quiet,-- Silient: Silent mode, does not output any information-F: Supports the use of fixed strings, does not support regular expressions, equivalent to FGREP-G,--basic-regexp: Supports the use of basic regular Expressions-P,--perl-regexp: Support for using the PCRE regular expression-e PATTERN,--regexp=pattern: Multi-mode mechanism-f file,--file=file:file for each line contains a PATTERN of text files, and grep script-a NUM,-- After-context=num: Display matches to the next few rows-B num,--before-context=num: Display matches to the previous lines-C num,-num,--context=num: Shows the front and back lines that match to


Examples of Use:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7C/FC/wKioL1beJpGhVp6FAAAn6dRIWf8841.png "title=" QQ picture 20160308090750.png "alt=" Wkiol1bejpghvp6faaan6driwf8841.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7C/FD/wKiom1beJomwD9J-AAAkdLmyeD8773.png "title=" QQ picture 20160308090958.png "alt=" Wkiom1bejomwd9j-aaakdlmyed8773.png "/>

Because grep and egrep use regular expressions, they are later combined with regular expressions.


3. The function of regular expressions and meta-characters


Regular Expressions a pattern written by a class of special characters and text characters, where some characters do not represent their literal meanings, but are used to denote control or the function of a wildcard.

Regular expression engine: A program that parses a given text using the regular expression pattern.

And the regular expression is divided into two categories:

(1) Basic Regular expressions

(2) Extended Regular expression


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7C/FC/wKioL1beMYbzBlVhAAUBUYyFMwk678.jpg "title=" Basic regular Expression meta-character. jpg "alt=" wkiol1bemybzblvhaaubuyyfmwk678.jpg "/>


note that the basic regular expressions and extended regular expressions are different!


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7C/FC/wKioL1beM0SAorC2AAVYxf0RAcg823.jpg "title=" Extended regular expression metacharacters. jpg "alt=" wkiol1bem0saorc2aavyxf0racg823.jpg "/>


Application of 4.grep command combined with regular expression


(1) Character matching

Find the line with Nologin in the/etc/passwd file

grep "[Nologin]"/etc/passwd


(2) Number of matches

Find the three-bit or four-digit number in the/etc/passwd file

Grep-o "\<[0-9]\{3,4\}\>"/etc/passwdegrep-o "\<[0-9]{3,4}\>"/etc/passwd


(3) Position anchoring

Displays lines in the/etc/passwd file that do not end with bash

Grep-v "bash$"/etc/passwd


(4) Group application

In/etc/passwd find the user whose user name is the same as the shell name

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


(5) Comprehensive application

Find the IP address in the ifconfig command result

Ifconfig | Egrep "\<inet[[:space:]]+.*[0-9]\>"

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

Egrep "^[[:space:]]+[^[:space:]]*"/etc/grub2.cfg

Find all lines in the/proc/meminfo file that begin with uppercase or lowercase c; At least three ways to implement

Egrep "^ (c| C) "/tmp/meminfogrep" ^[cc] "/tmp/meminfogrep-i" ^c "/tmp/meminfo




This article is from the "learning is a never-devalued investment" blog, please be sure to keep this source http://ch666.blog.51cto.com/10870222/1748685

grep Family and 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.