Linux Text Processing swordsman grep

Source: Internet
Author: User
Tags control characters printable characters expression engine

grep (Grep:global search REgular expression and print out of the line) is used as a text search tool to print lines that match to each line according to its pattern. Where pattern is the filter condition written by the regular expression metacharacters and text characters. The usage is relatively simple grep[options] PATTERN [FILE].
Applications such as:
One, 1. Want to match file containing R. T, the method can Grep-i ' R. T '/file path. It is not difficult to find which contains two parts 1 is the option to include which 2 is pattern. An important part of pattern is the combination of a meta-character and a literal character to form a regular expression.
2. For example, if you want to match R to T with only two letters or numbers, change patternfor [: Alnum:].
Grep-i ' R[:alnum:][:alnum:]t '. Similar pattern in character matching as
. Match any single character
[] matches any single character within the specified range
[^] matches any single character outside the specified range
[: Alnum:] Letters and numbers
[: Alpha:] represents any English uppercase and lowercase characters, i.e. A-Z, A-Z
[: Lower:] lowercase letter [: Upper:] Uppercase
[: Blank:] white space characters (spaces and tabs)
[: Space:] Horizontal and vertical white space characters (more than [: blank:] contains a wide range)
[: Cntrl:] non-printable control characters (backspace, delete, alarm ...) )
[:d igit:] decimal digits [: xdigit:] hexadecimal digits
[: Graph:] printable non-whitespace characters
[:p rint:] printable characters
[:p UNCT:] Punctuation
This enables the process of placing two unknown characters to two-bit letters or numeric characters in the middle of R and T.
Second, if there is a pattern of R, T appear the number of characters required, such as the need to match r any time after the selection pattern of letters. will be as follows Grep-i ' R [: alnum:] t '. Grep-i ' R [: alnum:] If you want to appear only once? T '. The meta-characters of the display count are as follows:

  • The

    matches the preceding character any time, including 0 times
    .
    any character of any length
    \? Matches its preceding character 0 or 1 times
    + matches its preceding character at least 1 times
    {n} matches the preceding character n times
    {m,n} Matches the preceding character at least m times, up to n times
    {, n} matches the preceding character up to n times
    {n,} matches the preceding character at least n times
    is used to specify the number of occurrences of the preceding class or character. Above like? + because it has a special meaning for the shell. So use a backslash \ to escape , take the meaning of its wildcard character without taking the meaning of the previous character, but give grep an explanation. The
    specific number of times can use {n} to indicate that the preceding character appears 0-n times; {m,n} represents m-n times.
    Thirdly, the need to find r at the beginning of the line or T in the line, you need to start the beginning or the end of the anchor. There is a
    ^ line anchor, which is used for the leftmost
    $ end of the pattern anchor, the rightmost
    ^pattern$ for pattern matching
    ^$ blank line
    ^[[:space:]]*$ blank line
    \< or \b The first anchor of the word, The left
    \> or \b suffix for the word pattern is anchored; the right
    \<pattern\> for the word pattern matches the entire word
    For example: Grep-i ' Crtl R [: alnum:]? T '
    If you want to do data analysis statistics at this time, use pipe | Wc-l to count several characters
    four, the grouping of regular expressions consists of strings joined together to represent one of the following

    Grouping: () binds one or more characters together as a whole, such as: (Root) +
    The contents of the pattern in the grouping brackets are recorded in internal variables by the regular expression engine, which are named: \1, \2, \3, ...
    \1 represents the character that matches the pattern between the first opening parenthesis and the matching closing parenthesis from the left
    such as: (string1+ (string2))
    \1:string1+ (string2)

    \2:string2
    Back reference: References the pattern in the preceding grouping brackets matches the character, not the pattern itself
    Or: |
    such as: A|b:a or B c|cat:c or cat (c|c) At:cat or cat
    and Crtl R This is still relatively easy to use, can be blurred to find/retrieve LS and other previously entered files Crtl E and Alt b

Linux Text Processing swordsman grep

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.