The Linux grep command is detailed

Source: Internet
Author: User


1.function

Linux the grep command in the system is a Powerful text-search tool , it can use regular expressions to search for text and print matching lines. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.

2.Syntax Format

grep [OPTION] ... PATTERN [FILE] ...

3.Main Parameters

[Options] Main parameters:

-C: Output matches only how many times.

-I: Case insensitive (only for single-character).

-H: The file name is not displayed when querying multiple files.

-L: Only file names that contain matching characters are output when querying multiple files.

-N: Displays matching lines and line numbers.

-S: does not display error messages that do not exist or have no matching text.

-V: Displays all lines that do not contain matching text.

-D: Skip search for subdirectories

-R: subdirectories are also searched


4.grepRegular Expression meta-character set (Base set):

\ : Ignores the original meaning of special characters in regular expressions.

^ : Matches the start line of the regular expression, such as: ' ^grep ' matches all rows beginning with grep.

$:   matches the end line of the regular expression, such as ' grep$ ' matches all rows ending with grep.

\< : Starts with the line that matches the regular expression. such as ' \<grep ' matches lines that contain words ending with grep.

\> : The end of the line to match the regular expression. such as ' grep\> ' matches lines that contain words ending with grep.

[]: Matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep. It is very special to note that in [] "only one character to be searched" is represented .

[N1-N2] : range, such as [A-z], that is, A, B, C to Z all meet the requirements.

. : All the individual characters.

* : There are characters, the length can be 0.

\(.. \): Tag matching characters, like ' (Love) ', and Love is labeled as 1.

\{n,m\}: meaning: Repeat the previous character n-m, if \{n\} is to repeat the previous character n times, if \{n,\} is repeated the previous word more than n times


5.used toEgrepand theGrep-ethe metacharacters(Expansion Sets)

+       matches one or more previous characters. such as: ' [a-z]+able ', matching one or more lowercase letters followed by able strings, such as loveable,enable,disable, etc.

? matches 0 or more previous characters. such as: ' Gr?p ' matches the GR followed by one or no characters, then the line of P.

A|b|c match A or B or C. such as: grep|sed matching grep or sed

() grouping symbols, such as: Love (able|rs) ov+ match loveable or lovers, matching one or more ov.


6.Example

parameter is not an example, see the name of meaning, the following main exercises regular expression


1 ) The use of regular expressions (filtering out native IP addresses)

[Email protected] lesson-1]# ifconfig eth0|grep ' inetaddr: ' |sed ' s/^.*addr:\ ([0-9]\{1,3\}\.[ 0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\] bcast.*/\1/g '

10.0.0.29

Parse: ^.* begins with any character and repeats 0 or more times

[0-9]\{1,3\} repeat to match 0-9 characters in 1 or 2 or 3 times

\ ( ... \) tag matching characters, first \ (\) marked as 1, second \ (\) marked as 2

\1 take the contents of the 1th tag

s/aa/b/g is a substitution function in sed, replace AA with B, and replace the matching line with the one in tag 1



2 ) Egrep Extended expression Application

Filter out multiple rows to use Egrep

[Email protected]]# ifconfig eth0|grep ' Inet|inet6 '

[Email protected]]# ifconfig eth0|egrep ' Inet|inet6 '

inet addr:10.0.0.29 bcast:10.0.0.255 mask:255.255.255.0

Inet6 ADDR:FE80::20C:29FF:FE6A:4121/64 Scope:link

Other +? () in the same vein, you need to use Egrep or grep-e






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.