Grep,egrep the corresponding regular expression usage.

Source: Internet
Author: User
Tags egrep

grep (Global search regular RE) is a powerful text search tool that only uses basic regular expressions to search for text and prints matching lines.

grep is a common and commonly used command, and its main function is to compare string data and then print out strings that match the user's needs, but the idea is that when grep looks for a string in the data, the data is filtered in the "Whole row" unit.


Example: grep--color=auto "Root"/etc/passwd


The Egrep command is equivalent to GREP-E, which allows you to search for text using an extended regular expression and print a string that meets the user's needs.

The grep command is equivalent to Grep-f, which uses a fixed string to search for text, but does not support a reference to a regular expression, so the command executes the fastest.

1.2. Basic usage of commands


grep [option] ' Search string ' filename

grep Common options:

-A: Search for data in a binary file as a text file



-C: Count the number of ' search strings ' found

-I: Ignore case

-V: Reverse lookup, which shows the line without the ' search string ' content

-O: Show only strings that are pattern-matched

-N: Output line number

--colour (color): Colour display

-A: Shows the following n lines matching to the word Funa

-B: Displays the first n rows matched to the word Funa

-C: Displays the front and back n lines matching to the word Funa

Auto


Always


Never


The:

. : Matches any single character

[]: matches any single character within the specified range

[^]: matches any single character outside the specified range

[0-9], [[:d Igit:]], [^0-9], [^[:d igit:]]

[A-z], [[: Lower:]]

[A-z], [[: Upper:]]

[0-9a-za-z], [[: Alnum:]]

[A-za-z], [[: Alpha:]]

Abbc,ac

Abb

Any length or any character

、? 0 or 1 times, indicating that there is no left.

"Ab/?c"

Abbc

Ac,abc

M times; indicates that its left character appears precisely M times c

At least m times, up to N times.

Ab\{2\}c

Ac,abc,abbbc

Abbc

Ab\{03\}c

Ac,abbbc

Abbbbbc

\{0,n\}: Up to n times;

\{m,\}: at least m times;

Location anchoring:

^: Anchoring the beginning of the line

^pattern

$: Anchor Line End

pattern$


^pattern$: Use the pattern to match the whole line;

# grep "^[[:space:]]*$"/etc/rc.d/rc.sysinit

^$: Match blank line;


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


\<: Anchor word head, can also be used \b

\<pattern, or \bpattern

# grep "\<sh"/etc/passwd


\>: Anchor ending, can also be used \b

Pattern\>, or pattern\b


\<pattern\>: Matches the entire word that PATTERN can match. # Ifconfig | grep "\<[0-9]\{2\}\>"


Create a new file for a, such as:

He like his lover.

He like his liker.

She Love her lover.

She Love her liker.


Practice:

1. Display the lines in the/proc/meminfo file beginning with uppercase or lowercase s, in two ways;

# grep "^[ss]"/proc/meminfo


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


3. Display the user whose default shell is/bin/bash in/etc/passwd file;

# grep "/bin/bash$"/etc/passwd | Cut-d:-f1


4. Find out one or two digits in the/etc/passwd file;

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


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

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


6, the display/etc/rc.d/rc.sysinit file with the beginning of #, 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



7, the Netstat-tan command execution results in the "LISTEN", followed by or with a blank character end of the line;

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


8. Add user bash, Testbash, basher, Nologin

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


This article is from "revived Nepal" blog, reprint please contact the author!

Grep,egrep the corresponding regular expression usage.

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.