Regular Expressions of Linux

Source: Internet
Author: User

Complaining is just an act of incompetence, without it. --Brother Ma, it's always a positive energy.


  • Regular Expressions:

    • Understanding: A pattern that is pieced together by a meta-character and a normal character, summed up by the corresponding "formula" based on certain characteristics of the target character or string

    • Function: can be used for file lookup, pattern matching, etc.

    • Character Matching:

      • .: Matches any single character

      • []: matches any single character within the specified range

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

    • Number of matches:

      • *: The characters on the left appear any time

      • . *: Any character of any length

      • \?: the character on its left appears 0 times or 1 this

      • \+: The character on the left side appears 1 or more times

      • \{m\}: Exact match to its left character appears m this

      • \{m,n\}: Matches its left character at least m times, up to N times

    • Location anchoring:

      • ^: Anchoring the beginning of the line

      • $: Anchor Line End

      • \<: Anchor Word first

      • \>: Anchor ending

      • ^$: Matches blank lines, including lines with only spaces and tabs!!!

      • \ (\): group. This feeling is relatively tall yet. In the grouping mode, the characters that are matched during a match are memorized temporarily by grep (saved to the built-in variable \1,\2 ... Medium), therefore

      • \1: Refers to the first parenthesis from left to right and the corresponding bracketed contents

  • Practice as follows:

    • Find the lines in the/proc/meminfo file that begin with uppercase s or lowercase s:

      • grep--color=auto "[^ss]"/proc/meminfo
    • Displays users and default shells whose default shell is non-/sbin/nologin in the/etc/passwd file

      • Grep-v "/sbin/nologin$"/etc/passwd | Cut-d:-f1,7


    • Displays one or two digits in the/etc/passwd file (not including three-bit and measurements-digits)

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

    • Displays lines in the/boot/grub/grub.conf file that begin with at least one white space character followed by the I character

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

    • Find information about users on your system whose user name and default shell are the same

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

    • D



This article is from the "Ops Dog" blog, make sure to keep this source http://yunweigou.blog.51cto.com/6299641/1627427

Regular Expressions of Linux

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.