Grep, egrep, and fgrep for basic regular expressions and text processing tools

Source: Internet
Author: User
Tags egrep

Grep, egrep, and fgrep for basic regular expressions and text processing tools

Grep (Global Search Regular Expression and print out the line global search Regular Expression and print out the rows) is a powerful text search tool, the commands of egrep and fgrep are only slightly different from those of grep. Linux uses the GNU version of grep, which is very powerful and can be passed through-G,-E, -F command option to use the functions of egrep and fgrep. grep works by searching string templates in one or more files. If the template contains spaces, it must be referenced, all strings after the template are treated as file names. The search results are sent to the screen without affecting the meta file content. The returned value for successful grep search is 0, and the returned value for unsuccessful search is 1, if the file to be searched does not exist, the return value is 2. Therefore, grep can be used for scripts to perform automated text processing.


Common grep parameters:

-I: case insensitive

-V: The result is opposite to the setting requirement.

-N: displays the row number in the output result.


Commonly used regular expressions:

^ Beginning of Line

$ End of line

[] Matching specified characters

[^] Match a character other than the specified one

\ <First word

\> End of a word

\ (A, \) .. \ n group and reference

^ $ Blank line

[0-9], [[: digit:] Any number

[A-Z], [[: lower:] Any lowercase letter

[A-Z], [[: Upper:] Any uppercase letter

[[: Alpha:] all uppercase and lowercase letters, matching all rows

[[: Alnum:] all uppercase and lowercase letters and numbers

[[: Space:] All blank characters

[[: Punct:] All punctuation marks

* Match the first character 0 or any times

| Or, for example, a | B

. * Match any character of any length

\? Match the previous character 0 times or once

\ {1 \} exact match times

The minimum number of matching times for \ {1, \}

\ {0, m \} can be matched for a maximum of three times

\ {N, m \} matches n to m times

GT>

Lt is less

GE is greater than or equal

Le is less than or equal

Ne is not equal

Equal to EQ

$0 script itself

$1 the first parameter of the script

$ *, All parameters referenced by [email protected]

$ # Format of the location parameter, number of rows



Grouping and reference: character groups are sorted by the Appearance Order of their left parentheses. For example, in an expression (1) (2 (3), there are four groups, the 0th group always represents the expression book:

1. (1) (2 (3 )))

2. (1)

3. (2 (3 ))

4. (3)


1. Character matching:

1. Used to match any single character. This character must be included and fixed.

[[Email protected] ~] # Grep 'ro. t'/etc/passwd

Root: X: 0: 0: Root:/root:/bin/bash

Operator: X: 11: 0: Operator:/root:/sbin/nologin


2. [] match any single character in it and match all rows

[[Email protected] ~] # Grep [WP]/etc/fstab

# See man pages fstab (5), findfs (8), Mount (8) and/or blkid (8) for more info

UUID = 16f1df10-ed2b-488b-9086-a48c60ab9ebe swap defaults 0 0

Tmpfs/dev/SHM tmpfs defaults 0 0

Devpts/dev/PTS devpts gid = 5, mode = 620 0 0

Proc/proc defaults 0 0

/Dev/CDROM/var/FTP/pub/iso9660 defaults, Ro, loop 0 0


3. [0-9], [[: digit:] indicates matching any number from 0-9, matching in the whole row

[[Email protected] ~] # Grep [[: digit:] 1.txt 1

64

676


4. [A-Z], [[: lower:] match any lowercase letter of A-Z and match the entire line.

[[Email protected] ~] # Grep [AE-Z] 1.txt # match letters A and E to Z to remove B and C

Hi

Hilucy

Asdxx


5. [A-Z], [[: Upper:] match any uppercase letter, Full Line Match

[[Email protected] ~] # Grep [[: Upper:] 1.txt

Hi

Hilucy


6. [[: Alpha:] all uppercase and lowercase letters, matching all rows

[[Email protected] ~] # Grep [[: Alpha:] 1.txt

Hi

Asdxx

2 W


7. [[: alnum:] All numbers and uppercase/lowercase letters, matching all rows

[[Email protected] ~] # Grep [[: alnum:] 1.txt

Hi

Hi

Hilucy

1

2fg


8. [[: Space:]. Therefore, it is a blank character and matches all rows.

[[Email protected] ~] # Grep [[: Space:] 1.txt

Asdxx

]

1 2

ASD


9. [[: punct:] matches all punctuation marks.

[[Email protected] ~] # Grep [[: punct:] 1.txt

,,

]

[] [

1.2


2. Times matching, used to match the number of characters

1. * match * The character above does not appear or appears any time. In this example, 2 does not appear or appears multiple times, that is, it starts with 1 and ends with 2, it can also be not followed by 2, even if there are still characters after 2, only matching the first 1 is matched and displayed, so it matches either two identical characters or no

[[Email protected] ~] # Grep '12 * '1.txt

1

1223

111

W1

I12

1.2


2 ,. * match from. start to include. contains 0 or any number of times. In this example, it starts with 12, followed by any number of times of any character, that is, the specified 12 must match, and the biggest difference is. * match the next character. If any character matches the next character, * match the first character. If the first character matches, two identical characters are displayed, if no match is available, cancel the previous character. In comparison, * the matching range is greater.

[[Email protected] ~] # Grep '12 * 1' 1.txt

12221

111

3 ,\? Match the first character 0 times or once. In this example, match 2 after 12, or once. The biggest difference with * Is \? A single character can appear for at most two consecutive times, and * can appear for countless times consecutively

[[Email protected] ~] # Grep '12 \? 1 '1.txt

111


4. \ {M \}; match the first character m times. The matching times must be completed.

[[Email protected] ~] # Grep -- color = auto'12 \ {3 \} '1.txt

12221

12222221


5. \ {M, \}: match the previous character at least m times

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/B0/wKioL1P-CmnwoYZYAACPUU4KZmw528.jpg "Title =" 5.png" alt = "wKioL1P-CmnwoYZYAACPUU4KZmw528.jpg"/>


6. \ {m, n}: match the previous character M-N times. In this example, the number of times that the first two are matched is 2, 3, and 4.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/ AE /wKiom1P-CYrgBT3tAABd3xokfPo748.jpg "Title =" 6.jpg" alt = "wKiom1P-CYrgBT3tAABd3xokfPo748.jpg"/>


7. \ {0, m \} \ match the preceding characters up to m times, that is, the number of times containing more than 0 times of the most correct times

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/B0/wKioL1P-Cquj-zYzAACD-rZ0TTY515.jpg "Title =" 7.jpg" alt = "wKioL1P-Cquj-zYzAACD-rZ0TTY515.jpg"/>


8. Match the combination of any two strings starting with r1r and ending with 32.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/ AE /wKiom1P-CbGhq3NpAABk78Sz0sg313.jpg "Title =" 8.jpg" alt = "wKiom1P-CbGhq3NpAABk78Sz0sg313.jpg"/>

3. Position anchoring: Specifies the specific position of the string to be matched in the row, such as the first, middle, or last position of a row.

1. ^ specifies the position of the matched string at the beginning of the row

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/ AE /wKiom1P-CjmxiP_BAABEKdGPIYw230.jpg "Title =" 1.jpg" alt = "wKiom1P-CjmxiP_BAABEKdGPIYw230.jpg"/>


2. $ specify the matched string position at the end of the row

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/B0/wKioL1P-C5GCmOlWAACM7N3m_dA950.jpg "Title =" 2.jpg" alt = "wKioL1P-C5GCmOlWAACM7N3m_dA950.jpg"/>


3. ^ $ indicates that the matched string is a blank row, and WC-L indicates the number of statistical rows, that is, 96 blank rows.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/ AE /wKiom1P-CvGzCuW2AAA4wvyWsBQ770.jpg "Title =" 3.jpg" alt = "wKiom1P-CvGzCuW2AAA4wvyWsBQ770.jpg"/>


4. \ <specify the first word of the matched string

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/B0/wKioL1P-DDqiMvygAACNnqytO-I998.jpg "Title =" 4.jpg" alt = "wKioL1P-DDqiMvygAACNnqytO-I998.jpg"/>

5. \> specify the end of the matched string

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/ AE /wKiom1P-CzbQbOI1AABvg7xMwiE281.jpg "Title =" 5.jpg" alt = "wKiom1P-CzbQbOI1AABvg7xMwiE281.jpg"/>

That is, specify the beginning and end of a word, which is equivalent to a fixed word.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/B0/wKioL1P-DGKiPpxAAABiVdlhp5E821.jpg "Title =" 51.jpg" alt = "wKioL1P-DGKiPpxAAABiVdlhp5E821.jpg"/>


6. \ (AB \) * XY \ 1 group and reference. \ 1 indicates the Matching content in the parentheses starting from the left.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/B0/wKioL1P-DHPBWF-UAABsiQDqV1M618.jpg "Title =" 61.jpg" alt = "wKioL1P-DHPBWF-UAABsiQDqV1M618.jpg"/>



This article is from the "Linux learning" blog, please be sure to keep this source http://zhangshijie.blog.51cto.com/806066/1545945

Grep, egrep, and fgrep for basic regular expressions and text processing tools

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.