Text processing tool: grep egrep fgrep

Source: Internet
Author: User
Tags egrep

1. grep: global search Regular Expression and print out the line.

The text search tool performs a row-by-row search on the target file based on the user-specified text mode to display the rows that can be matched by the mode.

Format: grep [Option] 'pattern' file1,... where pattern is a regular expression.

-- Color = Auto: The matched rows are highlighted.

-V: reverse match. lines that cannot be matched by the pattern are displayed.

-O: only the matched content is displayed, not the whole line.

-I (ignore-case): case insensitive

-A n: displays the matching rows and the following n rows.

-B N: displays the matching rows and the following n rows.

-C: display the context of the matching row


Its regular expression:

1. Character matching:

.: Any single character

[]: Any single character in the specified range

[0-9]:

[A-Z]:

[A-Z]:

[^]: Any single character out of the specified range


2. Number of times matching: used to specify the number of times of characters before the matching

*: Any number of times

. *: Match any character of any length

\? : 0 or 1 time

\ {M \}: matching m times

\ {M, N \}: Match m to n times

\ {M, \}: match at least m times

\ {M, N \}: match up to n times

3. Position anchoring: used to specify the position where a character appears

^: Pin the beginning of a line

$: End of the anchor row

^ $: Blank line

Example:

Count the rows ending with Bash in/etc/rc. d/rc. sysinit.

Grep -- color = auto'bash $ '/etc/rc. d/rc. sysinit

Count the number of empty rows in/etc/rc. d/rc. sysinit.

Grep -- color = auto' ^ $ '/etc/rc. d/rc. sysinit | WC-l

\ <CHAR: the beginning of the anchor.

Example: rows starting with R in/etc/passwd

Grep -- color = auto' \ <[RR] [^ [: punct:] * [TT] '/etc/passwd

Char \>: Specifies the end of a word.

Example: \ <H. O \> matches a word that starts with H and ends with O and contains any two characters

4. \ (\): Group

Example: ^ \ (AB \) * XY $: matches a string that starts with AB and ends with XY. The middle part contains the task AB.


5. Reference:

\ 1: backward reference, referencing the first left brace in front and the content of the pattern in the corresponding right Brace

\ 2:

......

For example, \ (A. B \) xy \ 1, which can match abxyab a6bxya6b and a6bxya7b.


For example, display the rows starting with case s in the/proc/meminfo File

# Grep "^ [ss]"/proc/meminfo

# Grep-I "^ s"/proc/meminfo

For example, if the default shell is not bash

# Grep-V "bash $"/etc/passwd | cut-D:-F1

For example, retrieve the user whose default shell is Bash and whose ID is the largest

# Grep "bash $"/etc/passwd | sort-n-t:-K3 | tail-1 | cut-D:-F1

For example, the/etc/rc. d/rc. sysinit file starts with #, followed by at least one blank character, and then contains at least one non-blank line.

# Grep "^ # [[: Space:] \ {1, \} [^ [: Space:] \ {1, \}"/etc/rc. d/RC. sysinit

For example, find one or two digits in the/etc/passwd file.

# Grep -- color = auto "\ <[0-9] \ {1, 2 \} \>"/etc/passwd

Example: Find the line ending with "listen" in the result of the netstat-tan command execution.

Netstat-tan | grep -- color "Listen [[: Space:] * $"

Example: Find the line ending with "listen" or "established" in the result of the netstat-tan command execution.


Ii. egrep: Build mode using extended regular expression class: equivalent to grep-e


Its regular expression:

1. Character matching:

.: Any single character

[]: Any single character in the specified range

[0-9]:

[A-Z]:

[A-Z]:

[^]: Any single character out of the specified range


2. Number of times matching: used to specify the number of times of characters before the matching

*: Any number of times

. *: Match any character of any length

+: Match the first character at least once.

? : 0 or 1 time

{M}: matching m times

{M, n}: Match m to n times

{M ,}: match at least m times

{M, n}: a maximum of N matching times

3. Position anchoring: used to specify the position where a character appears

^: Pin the beginning of a line

$: End of the anchor row

^ $: Blank line

Example:

Count the rows ending with Bash in/etc/rc. d/rc. sysinit.

Grep -- color = auto'bash $ '/etc/rc. d/rc. sysinit

Count the number of empty rows in/etc/rc. d/rc. sysinit.

Grep -- color = auto' ^ $ '/etc/rc. d/rc. sysinit | WC-l

\ <CHAR: the beginning of the anchor.

Example: rows starting with R in/etc/passwd

Grep -- color = auto' \ <[RR] [^ [: punct:] * [TT] '/etc/passwd

Char \>: Specifies the end of a word.

Example: \ <H. O \> matches a word that starts with H and ends with O and contains any two characters

4. (): Group

Example: ^ \ (AB \) * XY $: matches a string that starts with AB and ends with XY. The middle part contains the task AB.

|: Or


5. Reference:

\ 1: backward reference, referencing the first left brace in front and the content of the pattern in the corresponding right Brace

\ 2:

......

For example, \ (A. B \) xy \ 1, which can match abxyab a6bxya6b and a6bxya7b.


Example: Find the line ending with "listen" or "established" in the result of the netstat-tan command execution.

Netstat-tan | egrep -- color "(Listen | established) [[: Space:] * $"

For example, locate the integer from 1 to 255 in the result of executing the ifconfig command.

# Ifconfig | grep-e -- color = auto "\ <([1-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5]) |>"

Iii. fgrep: where F is fast and regular expressions are not parsed.


Text processing tool: grep egrep fgrep

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.