A detailed and regular expression of the grep command on the basics of Linux entry

Source: Internet
Author: User
Tags time 0 egrep

The grep command is one of the most frequently used commands under Linux, which filters the text according to the user-specified pattern (pattern) and displays the matching rows . Its command format is:

grep [OPTIONS] PATTERN [FILE]

For example: We are looking for the IP address configured in NIC 0 (the file path:/etc/sysconfig/network-scripts/ifcfg-eth0)---grep ' ipaddr '/etc/sysconfig/ Network-scripts/ifcfg-eth0

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/53/6E/wKiom1RnDl7xp3_jAABjlYv-3pI298.jpg "title=" Blog_ Pic1.png "alt=" Wkiom1rndl7xp3_jaabjlyv-3pi298.jpg "/>

(Note: Alias grep= ' grep--color=auto ' allows matching results to be highlighted)

several options (option) commonly used by grep are:

- V: Reverse selection , that is, the remaining rows after the display mode is filtered

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/53/6E/wKiom1RnD4Ljt_VCAAFSWmXCBcY497.jpg "title=" Blog_ Pic2.png "alt=" Wkiom1rnd4ljt_vcaafswmxcbcy497.jpg "/>

In addition to ipaddr this line is not displayed, other content is displayed to the user.

-I : ignore character case

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/53/6E/wKiom1RnENTws6cxAACp55bQKa0113.jpg "title=" Blog_ Pic3.png "alt=" Wkiom1rnentws6cxaacp55bqka0113.jpg "/>

The first command does not filter to anything, so the result is not shown, and the matching line is displayed after ignoring the case .

-o: Show only filtered content


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/53/6E/wKiom1RnE2yBkiubAAB1_bmXNYU501.jpg "title=" Blog_ Pic4.png "alt=" Wkiom1rne2ybkiubaab1_bmxnyu501.jpg "/>

Only content that is filtered according to the specified pattern is displayed.

Pattern represents the user given pattern, why this place we call him a pattern rather than a character, because regular expressions are usually used. The regular expression is a special kind of character, some characters of which usually do not represent the literal meaning, but as a control or globbing function. This type of character is called a meta-character. Regular Expressions are made up of these metacharacters, filtering the text along with grep and using it in later shell programming.

Regular expressions are divided into basic regular expressions and extended regular expressions in two categories.

The meta-character of a basic regular expression has the following characters:

of the filter character . Represents a match to anysinglecharacters. Then A.. B means there are two characters between A and B.

[] represents any single character within the set. A[BC] denotes ab or AC, note: ABC is also compliant with the mode

[^] denotes any single character outside of the matching set .

Special expression: [0-9] can be expressed by [[:d Igit:]]

[A-z] can be represented by [[: Lower:]]

[A-z] can be represented by [[: Upper:]]

[A-za-z] That is, all letters can be represented by [[: Alpha:]]]

[0-9a-za-z] That is, all letters and numbers can be represented by [[: Alnum:]]

Blank (Empty Characters, tab tab, etc.) denoted by [[: Space:]]

All punctuation marks are denoted by [[:p UNCT:]]

number of matches (qualifying for the number of occurrences of the preceding character)

* indicates a match * The preceding character appears any time 0,1,2 ..., then a*b indicates that a appears any time.

\? Indicates a match? The preceding character appears 0 or 1 times (\ Represents the escaped function only), then a\?b indicates that a appears 0 or 1 times

\{m\} indicates that the match {m} precedes the character M, then a\{2\} indicates a appears 2 times

\{m,n\} indicates that the preceding character of the match {m} appears at least n times, extending \{m,\} at least m times, \{0,n\} up to n times. then a\{1,2\} indicates that a appears at least 1 times up to 2 times.

\+ can also be expressed as a minimum of 1 occurrences

anchor Position (the character is positioned, the writing format is simple to remember: first left, tail right)

^ indicates that the beginning of a line is anchored, then ^a indicates that a must appear at the beginning of a row

$ means the end of the line is anchored, then a$ indicates that a row ends with a

\< indicates the first anchor of the word, then \<a indicates that a word begins with a

\> represents the ending anchor, then \>a indicates that a word ends with a

The grouping character divides the specified characters into groups, which are matched in the form of a group.

\ (\), then \ (ab\) indicates that AB is a group of

support back reference, \1,\2,\3 ... Represents the content from left to right that refers to its first # (start, #) ends directly.

An extended regular expression is used in the same way as a basic regular expression, except that there is no need to write an escape character on the writing format, which is more than the basic regular representation of a conditional selector:| That is, AB|CD represents AB or CD

I. Matches the character's have . []   [^]

Ii. number of Matches * ? +   {m} {m,n} {m,} {0,n}

III. The anchor has ^ $ \< or \b \> or \b

Iv. group character (with ) |

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/53/6F/wKiom1RnKHHThrgMAALXQRMNnws533.jpg "title=" Basic regular Expression meta-character. png "alt=" wkiom1rnkhhthrgmaalxqrmnnws533.jpg "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/53/6F/wKiom1RnKHaxlRLqAALczhbtQXo698.jpg "title=" Extends regular expression metacharacters. png "alt=" wkiom1rnkhaxlrlqaalczhbtqxo698.jpg "/>

Regular expression Exercises


1. Display the lines in the/proc/meminfo file that begin with uppercase or lowercase s;

Grep-i ' ^s '/proc/meminfo

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/53/6F/wKiom1RnKhmRTBOOAAFoqnMIgTU123.jpg "title=" Practice 1.png "alt=" Wkiom1rnkhmrtbooaafoqnmigtu123.jpg "/>

2. Display the default shell of the/etc/passwd file as a non-/sbin/nologin user;

Grep-v '/sbin/nologin$ '/etc/passwd | Cut-d:-f1

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/53/6F/wKiom1RnLGPzkZn2AAMPwGABOpA218.jpg "title=" practice 2_1. PNG "alt=" wkiom1rnlgpzkzn2aampwgabopa218.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/53/6E/wKioL1RnLNXCyOL4AADSI5TehD4660.jpg "title=" practice 2_2. PNG "alt=" wkiol1rnlnxcyol4aadsi5tehd4660.jpg "/>

3. Display the user whose default shell is/bin/bash in the/etc/passwd file; further: Display only the user whose ID number is the largest in the above results;

grep '/bin/bash$ '/etc/passwd | cut-d:-f1

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/53/70/wKiom1RnNvXCMYQhAAIKOOJgT44451.jpg "title=" practice 3_1. PNG "alt=" wkiom1rnnvxcmyqhaaikoojgt44451.jpg "/>

grep '/bin/bash$ '/etc/passwd | SORT-T:-k3-n | Tail-1 | Grep-o ' ^[[:alnum:]]\+ '

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/53/6E/wKioL1RnOR7z39IYAACTsL_zw04111.jpg "title=" practice 3_2. PNG "alt=" wkiol1rnor7z39iyaactsl_zw04111.jpg "/>4, find one or two digits in the/etc/passwd file;

grep ' \<[[:d igit:]]\{1,2\}\> '/etc/passwd


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/53/70/wKiom1RnOabiDkDpAAQJWwuWPy8691.jpg "title=" Practice 4.png "alt=" Wkiom1rnoabidkdpaaqjwwuwpy8691.jpg "/>

5. Display the line beginning with at least one blank character in/boot/grub/grub.conf;

grep ' ^[[:space:]]\+ '/boot/grub/grub.conf

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/53/6E/wKioL1RnOqDyfzNmAAFkv761xo8533.jpg "title=" Practice 5.png "alt=" Wkiol1rnoqdyfznmaafkv761xo8533.jpg "/>

6, display/etc/rc.d/rc.sysinit file, start with #, followed by at least one white space character, and then have at least one non-whitespace character line;

grep ' ^#[[:space:]]\+[^[:space:]]\+ '/etc/rc.d/rc.sysinit

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/53/6E/wKioL1RnOyeBbw8hAAT-XZzDGJ8241.jpg "title=" Practice 6.png "alt=" Wkiol1rnoyebbw8haat-xzzdgj8241.jpg "/>

7, find the Netstat-tan command execution results with ' LISTEN ' (can have white space characters) end of the line;

Netstat-tan | grep ' listen[[:space:]]*$ '

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/53/70/wKiom1RnPAfTTMGZAAFjQgrWnUY207.jpg "title=" Practice 7.png "alt=" Wkiom1rnpafttmgzaafjqgrwnuy207.jpg "/>


8, add user bash, Testbash, basher, Nologin (shell for/sbin/nologin), and find the current system on its user name and the default shell of the same user;

grep ' ^\ ([[: alnum:]]\+\). *\1$ '/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/53/6F/wKioL1RnPwXiPKd8AANQQkfNaog193.jpg "title=" practice 8_1. PNG "alt=" wkiol1rnpwxipkd8aanqqkfnaog193.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/53/70/wKiom1RnPpSxxdlzAAGZq301dCY689.jpg "title=" practice 8_2. PNG "alt=" wkiom1rnppsxxdlzaagzq301dcy689.jpg "/>

9. Extension question: Create a new text file, assuming the following:

He like his lover.

He love his lover.

He like his liker.

He love his liker.

Find out that the last word is a line made up of a previous word plus r.

# Nano Blog.text

# grep ' \ (\<[[:alpha:]]\+\>\). *\1r './blog.text

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/53/71/wKiom1RnQWnzPmiwAAD27e2CGno262.jpg "title=" Practice 9.png "alt=" Wkiom1rnqwnzpmiwaad27e2cgno262.jpg "/>

Use Egrep to complete the following exercises

Exercise 10: Display the default shell and user name for root, CentOS, or User1 users on the current system;

egrep ' ^root|centos|user1\> '/etc/passwd 650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/53/6F/wKioL1RnPwXiPKd8AANQQkfNaog193.jpg "title=" Practice 8_1.png "alt=" wkiol1rnpwxipkd8aanqqkfnaog193.jpg "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/53/6F/wKioL1RnQ8mx9DtoAAJsxVx30bE798.jpg "title=" Practice 10.png "alt=" Wkiol1rnq8mx9dtoaajsxvx30be798.jpg "/>

Exercise 11: Find the line after a word in the/etc/rc.d/init.d/functions file followed by a pair of parentheses "()";

Egrep ' \<[[:alpha:]]+\>\ (\) '/etc/rc.d/init.d/functions | Cut-d '-f1

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/53/6F/wKioL1RnROri0-UZAAJMqs4unas436.jpg "title=" Practice 11.png "alt=" Wkiol1rnrori0-uzaajmqs4unas436.jpg "/>

Exercise 12: Use echo to output a path, and use Egrep to remove its base name;

Echo/tmp/abc-/-s | Egrep-o ' [^/]+$ '

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/53/6F/wKioL1RnRgqxii8OAACe5deC5rY796.jpg "title=" Practice 12.png "alt=" Wkiol1rnrgqxii8oaace5dec5ry796.jpg "/>

Exercise 13: Find the value between 1-255 in the ifconfig command result;    

ifconfig | egrep ' \< ([1-9]|[ 1-9][0-9]|1[0-9]\+|2[0-4][0-9]|25[0-5]) \>'

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/53/6F/wKioL1RnR1-hRLtzAAOImfW-Bcw212.jpg "title=" Practice 13.png "alt=" Wkiol1rnr1-hrltzaaoimfw-bcw212.jpg "/>

Exercise 14: Find the IP address in the ifconfig command result (hint: 1.0.0.1-223.255.255.254)

Ifconfig | Egrep-o ' (\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \>\.\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \>\.\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \>\.\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \>) '

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/53/71/wKiom1RnSqOiAnWfAAEfSkflq9s005.jpg "title=" Practice 14.png "alt=" Wkiom1rnsqoianwfaaefskflq9s005.jpg "/>

Experience: It's easier to understand the text content you want to search all as a single character.

Extension: There is also a quick search tool fgrep, but he does not support regular expressions and is therefore seldom used.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/53/71/wKiom1RnSvHAtu-YAAKu9MmdRB4986.jpg "title=" grep, Egrep, Fgerp.png "alt=" Wkiom1rnsvhatu-yaaku9mmdrb4986.jpg "/>


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

A detailed and regular expression of the grep command on the basics of Linux entry

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.