grep Command Learning

Source: Internet
Author: User

Introduction to 1.GREP commands

the grep command is a very powerful text Search tool in the Linux system, full name global Regular expression Print, which represents the globally regular expression version, which is used by all users. According to a pattern ( pattern) to search for text and display the line of text that conforms to the pattern. The pattern: a matching condition that consists of a combination of metacharacters of the text Word wildcards regular the expression .

2. Command format

grep [Options] PATTERN [FILE ...]

Options option:

-I ignore pattern case

--color with color tags

-V Reverse Lookup

-O displays only strings that are matched by the pattern

-C outputs only the count of matching rows

-N Displays matching lines and line numbers

-V Displays all lines that do not contain matching text

Pattern field:

Regular Expressions:

    • Match any single character "." grep ' R.. T '/etc/passwd
    • Match the previous character any time "*"
    • Match any character of any length ". *" grep ' a.*b '/333/testre
    • Matches the previous character 1 or 0 times "\?" grep ' A\?b '/333/testre
    • Match the preceding character at least N, up to M "\{n,m\}" grep ' A\{1,3\}b '/333/testre

    • The position is defined as the beginning of the line "^", grep ' ^r. T '/etc/passwd
    • Position defined as Line end "$", grep ' w$ '/etc/initab
    • Blank line "^$", grep ' ^$ '/etc/fstab | Wc-l
    • Matches a single character "[]" in a specified range
    • Matches a single character "[^]" outside the specified range
    • [[:d Igit:]] [[: Lower:]] [[: Upper:]] [[: Space:]] [: Alpha:]] [[: Alnum:]]

For example: Match lines that end with a white space character + number? grep ' [[: space:]]*[[:d igit:]]$ '/etc/initab

    • Any character behind it appears as the first word "\<"
    • Any character in front of the word "\>" appears as the header
    • Grouped content "\ (\)" grep "\ (ab\) *"/333/testre

Basic Regular Expressions:

    • Character match: "." "[]" "[^]"
    • Number of matches: "*" "\?" "\{m,n\}" ". *"
    • Location matching: "^" "$" "\<" "\>" "\b" "\ (\) \1 \2 \3"

To extend the regular expression:

GREP-E/Egrep

Number of matches "*"? "+" its pre-character at least once grep-e ' ^[[:space:]]+ '/boot/grup/group.conf '

"|" or grep-e ' C|cat '/333/testre

Example: Find all of the 1-255 integers in/etc/networks

Grep-e--color ' \< ([1-9] | [1-9] [0-9] | 1[0-9][[0-9] | 2[0-4][0-9] | 25[0-5] '/etc/networks

grep Command Learning

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.