Linux Text processing the grep command of the Three Musketeers

Source: Internet
Author: User

Linux Text processing the Three Musketeers grep

grep: Text filter (Pattern: pattern) Tool

grep, Egrep, fgrep (regular expression search not supported)

Sed:stream Editor, text editing tools

Implementation Gawk on Awk:linux, Text Report Generator

Grep
    • Grep:global Search RegularExpression and Print out of the line

Function: The text Search tool, according to the user-specified "mode" to match the target text line by row to check; print matching lines

Patterns: Filter conditions written by regular expression characters and text characters

    • grep [OPTIONS] PATTERN [FILE ...]

grep root/etc/passwd

grep "$USER"/etc/passwd

grep ' $USER '/etc/passwd

grep ' WhoAmI '/etc/passwd

    • grep command Options

--color=auto: Shading to matching text (centos6 default, 7 default plus)

-V: Displays rows that are not matched by pattern

-I: Ignore character case

-N: Show matching line numbers

-C: Count the number of matching rows

-O: Show only the matching string

-Q: Silent mode, does not output any information

-A #: After, followed by # lines

-B #: Before, Front # line

-c #:context, front and back # lines

-E: Implementing a logical or relationship between multiple options

Grep–e ' Cat '-e ' dog ' file

-W: Matches entire word

The word number underscore cannot be used as a delimiter for a word, others can

-E: Use ere

-F: Equivalent to Fgrep, does not support regular expressions

Regular expressions

Match a string in a file

    • REGEXP: A pattern written by a class of special characters and text characters, in which some characters (metacharacters) do not represent literal meanings, but are functions that represent control or a wildcard
    • Program support: Grep,sed,awk,vim, less,nginx,varnish, etc.
    • Divided into two categories:
    1. Basic Regular Expressions: BRE
    2. Extended Regular expression: ERE

GREP-E, Egrep

    • Regular expression engine:

Using different algorithms to check the software module for processing regular expressions

PCRE (Perl Compatible Regular Expressions)

    • Meta-character classification: character matching, number of matches, position anchoring, grouping
    • Man 7 regex
Basic Regular Expression metacharacters (different from wildcard characters)
    • Character Matching:

. Match any single character

[] matches any single character within the specified range

[^] matches any single character outside the specified range

^ put in [] inside and outside are different concepts

[: 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

    • Number of matches: used after the number of characters to be specified, to specify the number of occurrences of the preceding character

* match the preceding character any time, including 0 times

Greedy mode: Match as long as possible

. * Any character of any length

\ match its preceding character 0 or 1 times

\+ matches the characters in front of it 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

A\|bcd=a or BCD

\ (a\|b\) CD=ACD or BCD

    • Position anchoring: positioning where it appears

^ Beginning of the line anchor, for the leftmost mode

$ line End anchor for the right side of the pattern

^pattern$ for pattern matching entire row

^$ Empty Line

^[[:space:]]*$ Blank Line

\< or \b The first anchor for the left side of the word pattern

\> or \b ending anchor; for the right side of the word pattern

\<pattern\> matches the entire word =-w

    • Group: \ (\) binds one or more characters together and treats them 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
    • Example: \ (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: \|

Example: A\|b:a or b c\|cat:c or cat \ (c\|c\) At:cat or cat

Egrep and extended Regular expressions
    • egrep= GREP-E
    • Egrep[options] PATTERN [FILE ...]
    • Extend the metacharacters of regular expressions:
    • Character Matching:

. Any single character

[] Specify the range of characters

[^] characters not in the specified range

Extending regular Expressions
    • Number of matches:

*: matches the preceding character any time

?: 0 or 1 times

+:1 Times or more

{m}: matches M-Times

{M,n}: At least m, up to N times

    • Location anchoring:

^: Beginning of the line

$: End of line

\<, \b: the first language

\>, \b: The end of the language

    • Group:

()

Back reference: \1, \2, ...

    • Or:

A|b:a or B

C|cat:c or Cat

(c|c) At:cat or cat

    • Appendix:

~] #ls | grep *

Enter LS to list A file that starts with a, and it will have a line, and the default does not enter the directory. You need to add a "a *" to the regular expression

Linux Text processing the grep command of the Three Musketeers

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.