Use regular expressions in Linux commands

Source: Internet
Author: User

When using the grep, awk, and sed commands, you must use a regular expression.
For example, I used grep to find out whether there was any error in the code compilation result. Or whether there is an error with my code.
Here we will talk about the basic application of Regular Expressions:

• Match the beginning and end of a row.
• Match a dataset.
• Match only letters and numbers.
• Match string sets within a certain range.

 

^ Match only the beginning of a row
$ Match only the end of a row
* One single character followed by *, matching 0 or more of the single characters
[] Match the character in []. It can be a single character or a character sequence. You can use-to indicate the range of character sequences in []. For example, use [1-5] instead of [12345].
\ It is used to block the special meaning of a metacharacter. Sometimes some metacharacters in shell have special meanings. \ Can make it lose its meaning.
. Match any single character
Pattern \ {n \} Used to match the occurrence times of the previous pattern. N is the number of times
Pattern \ {n ,\} The meaning is the same as above, but the minimum number of times is N.
Patter \ {n, m \} The meaning is the same as above, but pattern appears between N and M.

  1. Match a single character with a period
    The period "." can match any single character. For example, if you want to match a string, start with be g and
    Any character, which can be expressed as be G. N, "." can match the string header or any character in the middle.
    In the LS-l command, you can match certain permissions:
    ... X.. x. x.. x
    This format matches the execution permissions of users, user groups, and other group members.
    ~ $ LS-L | grep... x. x.. x
    Drwxrwxrwx 1 h00209633 domain u 0 January 19 08:38 Bin
    Drwxrwxrwx 1 h00209633 domain u 0 February 7 08:06 Emacs
    Drwxrwxrwx 1 h00209633 domain u 0 January 19 08:38 etc
    Drwxrwxrwx 1 h00209633 domain u 0 January 19 08:38 info
    Drwxrwxrwx 1 h00209633 domain u 0 January 19 08:38 leim
    Drwxrwxrwx 1 h00209633 domain u 0 February 7 09:05 LISP
    Drwxrwxrwx 1 h00209633 domain u 0 February 7 08:07 site-lisp
    ~ $
  2. Match strings or character sequences at the beginning of a row with ^
    ^ Only matching characters or words at the beginning of a line are allowed. For example, use the LS-l command and match the directory. Yes
    This is because the first character of each line in the LS-l command result is D, which represents a directory.
    ~ $ LS-L | grep ^ d
    Drwxrwxrwx 1 h00209633 domain u 0 January 19 08:38 Bin
    Drwxrwxrwx 1 h00209633 domain u 0 February 7 08:06 Emacs
    Drwxrwxrwx 1 h00209633 domain u 0 January 19 08:38 etc
    Drwxrwxrwx 1 h00209633 domain u 0 January 19 08:38 info
    Drwxrwxrwx 1 h00209633 domain u 0 January 19 08:38 leim
    Drwxrwxrwx 1 h00209633 domain u 0 February 7 09:05 LISP
    Drwxrwxrwx 1 h00209633 domain u 0 February 7 08:07 site-lisp
  3. Match strings or characters with $ at the end of a row
    It can be said that $ is the opposite of ^. It matches strings or characters at the end of a row, and $ is placed after matching words. Assume that
    All rows at the end of the lisp statement. The operation is as follows:
    ~ $ Ls | grep SP $
    LISP
    Site-lisp
    ~ $
  4. Use * to match a single character in a string or its recurring series
    This special character is used to match repeated expressions of any character or string. For example:
    Find E * m (string starting with E and ending with m)
    ~ $ Ls | grep E * m
    Emacs
    Leim
    ~ $
  5. Use \ to block the meaning of a special character
    Sometimes you need to find some characters or strings that contain a character specified by the system as a special character. What
    Is it a special character? In general, the following characters can be considered special characters:
    $. '"* [] ^ | {}\+?
    For example, find a file with A. El suffix.
    ~ /Lisp $ ls | grep \. El $
    Abbrev. El
    Abbrevlist. El
    Add-log.el
    Align. El
    Allout. El
    Ansi-color.el
    Apropos. El
    Arc-mode.el
    Array. El
    Autoarg. El
    Autoinsert. El
    Autorevert. El
    Avoid. El
    Battery. El
    Bindings. El
    Bookmark. El
    BS. El
    Buff-menu.el
    Button. El
    Calculator. El
    Case-table.el
  6. Use [] to match a range or set
    Use [] to match a specific string or string set. You can use commas to separate different strings to be matched in the arc,
    This is not mandatory (some systems advocate using commas in complex expressions). This increases the readability of the mode.
    .
    "-" Indicates a string range, indicating that the string range starts from the character on the left of "-" to the right "-".
    .
    If you are familiar with a string matching operation, you should often use the [] mode.
    If you want to match any number, you can use:
    [0123456789]
    However, you can use the "-" symbol to simplify the operation:
    [0-9]
    Or any lowercase letter
    [A-Z]
    To match any letter, use:
    [A-Za-Z]
    Indicates the letter range from a-Z and A-Z.
    To match any letter or number, the mode is as follows:
    A-Za-z0-9
    Note the use of the ^ symbol. When it is directly used in the first bracket, it means that the content in the negative or mismatched brackets.
    [^ A-Za-Z]
    Match any non-letter character while
    [^ 0-9]
    Match any non-numeric character.
    In the last example, we can guess that in addition to using ^, there are also some methods to search for any special character.
  7. The number of times the matching result is displayed in the \ {\} match mode.
    Use * to match all matching results at any time. However, if you specify the number of times, use \ {\}. There are three modes:
    Format:
    Pattern \ {n \} match mode appears n times.
    Pattern \ {n, \} match mode appears at least N times.
    Pattern \ {n, m} match mode appears between N to m times, n, m is 0-2 5 any integer in 5.
    For example, the format is as follows: the first four characters are numbers, followed by XX, and the last four are also numbers,
    The procedure is as follows:
    [0-9] \ {4 \} XX [0-9] \ {4 \}
    The specific meaning is as follows:
    1) The matching number appears four times.
    2) followed by code xx.
    3) the number appears four times.

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.