Linux grep command

Source: Internet
Author: User
Tags expression engine

First, the role:

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

A text search tool that matches the target text line by row according to the user-specified pattern (filter condition);


Second, common options:

grep [OPTIONS] PATTERN [FILE ...]

grep [OPTIONS] [-E PATTERN |-f FILE] [FILE ...]


OPTIONS:

--color=auto: Color The matched text to highlight;

-i:ignorecase, ignoring character case;

-O: Displays only the string that matches to itself;

-V: Reverse display;

-E: Supports the use of extended regular expression metacharacters;

-q,--quiet,--silent: Silent mode, that is, do not output any information;


-A #:after, after # line

-B #:before, front # line

-c #:conext, front and back # lines


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;


Number of matches: used to limit the number of occurrences of the preceding character, after the character to specify the number of occurrences, and the default work is in greedy mode;

*: Matches its preceding character any time, 0, 1, multiple times;

. *: Matches any character of any length;

\?: matches the preceding character 0 or 1 times, that is, the preceding character is optional;

\+: Matches the preceding character 1 or more times, that is, the preceding character must appear at least 1 times;

\{m\}: Matches the preceding character m times;

\{m,n\}: Matches its preceding character at least m times, up to n times;

\{0,n\}: Up to n times;

\{m,\}: at least m times;


Location anchoring:

^: Anchor at the beginning of the line; to the left of the pattern; ^root

$: End of line anchoring; for the rightmost side of the pattern;

^pattern$: Used for PATTERN to match whole line;

^$: blank line;

^[[:space:]]*$: A blank line or a line containing white space characters;

Word: A continuous character (string) consisting of a non-special character is called a word;


\< or \b: The first anchor of the word, used for the left side of the word pattern;

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

\<pattern\>: matches complete words;

Grouping and referencing:

\ (\): Bind one or more characters together and treat them as a whole;

Note: The contents of the pattern matching in the grouping brackets are automatically recorded in the internal variables by the regular expression engine, and these variables are:

\1: The pattern from the left side, the first opening parenthesis and the matching closing parenthesis, matches the character of the pattern;

\2: The pattern from the left side, the second opening parenthesis, and the matching closing parenthesis to the character;

\3: The pattern from the left side, the third opening parenthesis, and the matching closing parenthesis match the character to the pattern;

...

grep "\ (L.. e\). *\1 "Love.txt behind the \1 can reference \ (l: e\) in the content;


Back reference: References the pattern in the preceding grouping brackets to the character;


grep [OPTIONS] [-E PATTERN |-f FILE] [FILE ...]

Options:-I,-O,-V,-Q,-A,-B,-C

-G: Support for basic regular expressions


Extend the metacharacters of regular expressions:

Character Matching:

Number of matches:

*: Any time, 0,1 or multiple times;

? : 0 or 1 times, the preceding character is optional;

+: Its preceding character at least 1 times;

{m}: its preceding character m times;

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

{0,n}:

{m,}

Location anchoring:

Grouping and referencing:

(): grouping: Pattern matching in parentheses the hand character is recorded hermetical the internal variables of the expression engine;

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

Or:

A|b:a or B:

C|cat:c or Cat

(c| C) At:cat or cat

Examples of commands:


  Find out the number of blank lines in the/etc/fstab file      ~]# grep  "^$"  /etc/fstab  |  wc -l        1    find the user-root row in the/etc/passwd file     ~]# grep  "^root\>"  /etc/passwd         root:x:0:0:root:/root:/bin/bash    find the line in the/etc/passwd file with the same user name as the shell name       ~]# grep --color -E  "^ ([^:]+\>). *\1$"  /etc/passwd         sync:x:5:0:sync:/sbin:/bin/sync         shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown        halt:x:7:0:halt:/ sbin:/sbin/halt    find the line that starts with M or s in the/proc/meminfo file      ~]# grep -e   "^ (m| S) " /proc/meminfo         MemTotal:          808160 kB        MemFree:           488092 kB         swapcached:            0 kb         Mlocked:                0 kB        SwapTotal:        2072572 kB        SwapFree:         2072572 kB        Mapped:             15460 kB         Shmem:                236 kb        slab:               75108 kB        SReclaimable:       16084 kB        SUnreclaim:         59024 kB    displays a line that starts with a letter in the/etc/inittab file       ~]# grep  "^[a-z"  /etc/inittab          id:3:initdefault:    find the integer between 1-255 in the ifconfig command result      ~]#  ifconfig | grep -e --color  "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> "      eth0  Link  encap:ethernet  hwaddr 00:0c:29:12:ba:26             inet addr:172.16.249.45  bcast:172.16.255.255&NBSP;&NBSP;MASK:255.255.0.0&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;INET6&NBSP;ADDR:  fe80::20c:29ff:fe12:ba26/64 Scope:Link           up broadcast running multicast  mtu:1500  metric:1           RX bytes:23544203  (22.4&NBSP;MIB)   tx bytes :14827318  (14.1&NBSP;MIB)           inet addr :127.0.0.1  mask:255.0.0.0          inet6  Addr: ::1/128 scope:host          up loopback  running  mtu:65536  metric:1


Linux grep command

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.