The grep and egrep of the Three Musketeers of Linux

Source: Internet
Author: User
Tags expression engine egrep

Grep:the Three Musketeers of text processing on Linux
1 grep: Text filter (Pattern: pattern) tool; *(grep, Egrep, Fgrep)2sed:stream Editor, text Editing tool;3 awk:linux implementation gawk, Text Report Generator;

Grep:global search REgular expression and Print out of the line.

Function: Text Search tool, according to user-specified "mode" to match the target text line by row to check; print matching lines;Pattern: The filter condition written by the regular expression character and the text character;REGEXP: A pattern written by a class of special characters and text characters, some of which do not represent the literal meaning of a character, but are a function of control or wildcard;divided into two categories:Basic Regular Expressions: BREextended Regular expression: EREgrep-e, EgrepRegular expression engine
1 Options:2--color=Auto: Coloring the text to match to the display;3-V: Displays rows that cannot be matched to the pattern;4-I: ignoring character case;5-o: Show only the matching string;6-Q: Silent mode, do not output any information;7-A #:after, after # line8-B #: Before, front # line9-C #:context, front and back # linesTen  One-E: Use ere;

Basic regular Expression meta-characters:

Character Matching:

.: matches any single character;[]: matches any single character within the specified range[^]: matches any single character outside the specified range[:d Igit:], [: Lower:], [: Upper:], [: Alpha:], [: Alnum:], [:p UNCT:], [: Space:]number of matches: used after the number of characters to be specified, to specify the number of occurrences of the preceding character;*: Matches the preceding character any time;For example: grep "X*y"AbxyXayXxxxxxyGreedy mode. *: Any character of any length;\?: matches the preceding character 0 or 1 times, i.e. the preceding is optional;\+: Matches its preceding character at least 1 times;\{m\}: Matches the preceding character m times;\{m,n\}: Matches the preceding character at least m times, up to n times;\{0,n\}: Matches the preceding character up to n times;\{m,\}: Matches the preceding character at least m times;Location Anchoring:^: anchor at the beginning of the line, for the leftmost mode;$: End of line anchoring; for the rightmost side of the pattern;^pattern$: Used for pattern matching whole line;^$: Empty line;^[[:space:]]*$\< or \b: The first anchor of the word, the left side of the word pattern ;\> or \b: the ending anchor; for the right side of the word pattern ;\<pattern\>: matches the whole word; Group:\ (\): Bind one or more characters together and treat them as a whole;\ (xy\) *abNote: 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: From the left side, the first opening parenthesis and matching the right parenthesis to match the pattern between the characters;\ (ab\+\ (xy\) *\):\1:ab\+\ (xy\) *\2:xyBack reference: references the pattern in the preceding grouping brackets to match the character (not the pattern itself) Practice Examples : 1. Display the line in the/proc/meminfo file that begins with the size S; (Requires: use two ways)
1 ' ^{s| S}' /proc/meminfo2'^s'  /proc/meminfo

2. Display the lines in the/etc/passwd file that do not end in/bin/bash;

1 ' /bin/bash$ '  /etc/passwd

3. Display the user name of the user with the largest ID number in the/etc/passwd file;

1 # sort-t:-k3-n/etc/passwd | Tail-1 | Cut-d:-f1

4, if the user root exists, display its default shell program;

1 # ID root &>/dev/null"^root\>" /etc/passwd | Cut-d:-f7

5. Find out the number of two or three digits in/etc/passwd;

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

6. Display a line in the/etc/rc.d/rc.sysinit file that begins with at least one white space character and that is followed by a non-whitespace character;

1 " ^[[:space:]]\+[^[:space:]] " /etc/rc.d/rc.sysinit

7. Find the line ending with ' LISTEN ' followed by 0, 1, or more whitespace characters in the result of the "Netstat-tan" command;

" listen[[:space:]]*$ "

8, add user bash, Testbash, basher and Nologin (its shell is/sbin/nologin), and then find the/etc/passwd file user name with the shell name of the line;

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

Egrep and extended Regular expressions

Egrep = Grep-e

Extend the metacharacters of regular expressions:Character matching: . [] [^]number of matches: *?: 0 or 1 times;+: 1 or more times;{m}: matches m times;{m,n}: At least m, up to n times;Anchoring: ^ $\<, \b\>, \bGroup: ()Back reference: \1, \2, ...or:a|bC|cat:c or Cat Practice Examples : 1. Display the default shell and UID of the current system root, CentOS or User1 user;
1 ' ^ (root|centos|user1) \> ' /etc/passwd | cut-d:-f1,3,7

2, find the/etc/rc.d/init.d/functions file (CENTOS6) A word followed by a parenthesis line;

" ^[_[:alpha:]]+\ (\) " /etc/rc.d/init.d/functions

3, use echo output an absolute path, use Egrep to remove its base name;

1 " /MNT/SDC " " [^/]+/?$ " | cut-d"/" -f1

The grep and egrep of the Three Musketeers 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.