Shell Regular expression (1)

Source: Internet
Author: User
Tags egrep

First, what is the regular

A regular is a way to describe a character or string by combining symbols with special meanings, called regular expressions. Or, the regular is the rule used to describe a class of things.

Second, grep

1. Parameters

-N: Show line numbers
-O: Show only matching content
-Q: Silent mode, no output, you have to use $? To determine the success of the execution, that is, there is no filtering to the desired content

-L: If the match succeeds, only the file name is printed, the failure is not printed, usually-rl together, grep-rl ' root '/etc
-A: If the match is successful, the matching row and the subsequent n rows are printed together
-B: If the match succeeds, the matching row and its first n rows are printed together
-C: If the match succeeds, the matching row and its n rows are printed together
--color
-C: If the match succeeds, the number of rows to match is printed
-e: Equals Egrep, extended
-I: Ignore case

-V: Inverse, mismatch
-W: Match word

2.grep type
Grep
Fgrep
Pgrep
Egrep

3. Regular introduction

^ Beginning of the line
$ End of line
. Any single character other than the line break
* 0 or more of the leading characters
. * All Characters
[] Any character within a group of characters
[^] reverse each character within a character group (does not match each character in a group of characters)
^[^] lines that begin with characters within a non-character group
[A-z] lowercase letter
[A-z] capital letters
[A-z] lowercase and uppercase letters
[0-9] Number
\< words are usually separated by spaces or special characters, and successive strings are treated as words.
\> Word Tail

Extended regular sed plus-r parameter or escape
grep plus-E or egrep or escaped

grep Homework Examples

1. Displays all the rows that contain root:

grep ' Root '/etc/passwd

2. Output any rows that contain bash, and also output the contents of the next two lines, followed by the row:

GREP-C2 ' bash '/etc/passwd

3. Shows how many lines contain nologin.

Grep-c ' Nologin '/etc/passwd

4. Shows that the rows contain root and the line number is output.

Grep-n ' Root '/etc/passwd

6. New user

Abominable

Abominate

Anomie

Atomize

Write regular expressions and match them up

Egrep a.omi.*e/etc/passwd

7. Built four users

Alex213sb

wpq2222b

Yh438pig

egon666

Egon

Filter out the user name consists of a letter + number + letter Line

Egrep ' ^[a-z]+[0-9]+[a-z]+ '/etc/passwd

8. Displays all filenames containing root in/etc directory

Grep-rl ' root '/etc

9. Filter out all comments and all blank lines in/etc/ssh/sshd_config

Grep-v ' ^# '/etc/ssh/sshd_config |grep-v ' ^ *$ '

Shell Regular expression (1)

Related Article

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.