Simple use of regular expressions on the Gnu/linux platform

Source: Internet
Author: User

Friendly reminder: This blog is involved in the content of the system practices involved in the implementation of Centos6.5, Gnu/linux referred to as linux,gnu/grep abbreviation for the grep,gnu/sed abbreviation for the Sed,gnu/gawk abbreviation for awk.

------------------------------------------------- Wedge ----------------------------- ----------------

Tavern a corner, a fat three thin around square table seated, big fast ear yi Hou is chit chat when.

Skinny A: "Fat, why should you associate regular expressions with Linux?" ”

Fat: "Because the following is a simple use of regular expressions on Linux platforms. ”

Skinny B: "Do you think the regular expressions on the Linux platform are important?"

Fat: "The importance does not have to question, Linux two major features: ① all documents." ② use a text file to save the service configuration. It determines the regular expression and its three good base friends, the combination of Grep/sed/awk in processing the file search, replace, generate report file, no go to disadvantage. ”

Skinny c: "Fat, you don't start to talk about, this meal you please." ”

Fat: "Let me in a word, the use of the regular expression on Linux, is a write rotten theme, if there is similar, purely coincidental, cattle people please self-flash, about the rack please first to the public security record." ”

----------------------------------Body----------------------------------------------

Please allow me to briefly introduce the tool of grep, or the regular expression cannot be demonstrated.

1. grep is a search tool that can search for text or pipe-by-line data based on the user-defined Patten, and if there is a string matching patten pattern in the row, the row is printed (grep default behavior). This patten can be a string, or a pattern that uses a combination of regular expression meta-characters.

For example, search the/etc/password file for rows that contain the root string.

[[email protected] test]$ grep--color=auto ' root '/etc/passwd
root: x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[Email protected] test]$

See if it is convenient to search out the line containing the root string?!

Syntax format for 2.grep:

grep [option] ' PATTEN ' [FILE ...]

Common options:

--color=auto: Use this option to make matching strings appear discolored.

-E: Use an extended regular expression.

-O: Displays only strings in the row that match the pattern, not other content.

-V: Displays the rows that do not contain a matching pattern string.

-I: Ignores character case when matching.

-B #: #是数字, displays matching rows and lines above the row.

-C #: #是数字, displays matching lines and line up and down lines.

-A #: #是数字, displays matching lines and lines below the line.

* Other options please use $man grep for your own viewing.

* Mode Patten is recommended to use the "number or" "number enclosed.

Example:

Generate Test files and content:

[Email protected] test]$ Cat<<eof>t1file.txt
> Who is you?
> My name is Brad Pitt.
> Your Phone number is:135456789
> Yes,sir
> EOF

[Email protected] test]$

2.1) Show grep default behavior: Search for rows containing you string

[[email protected] test]$ grep ' You ' t1file.txt
Who is?
[Email protected] test]$

By default, only rows that contain you are displayed, lines that do not contain the you string are not displayed, and you do not change color.

2.2) Implement matching string color display: Search for rows containing you string

[[email protected] test]$ grep--color=auto ' You ' t1file.txt
Who is ?
[Email protected] test]$
This can be seen clearly and easily identifiable.

2.3) Ignore character case: Search for line containing you string

[Email protected] test]$ grep-i--color=auto ' You ' t1file.txt
Who is ?
you r phone number is:135456789
[Email protected] test]$

After using option-I, the search scope is further increased.

2.3) Test-V: Search for rows that do not contain a string of you

[Email protected] test]$ grep-v-i ' You ' t1file.txt
My name is Brad Pitt.
Yes,sir
[Email protected] test]$

[Email protected] test]$ grep-v ' You ' t1file.txt
My name is Brad Pitt.
Your Phone number is:135456789
Yes,sir
[Email protected] test]$

In order to see the number of options used, there is no merge-V and-I are used.

2.4) test the option-c#: Displays a row containing the name string, and 1 rows above and below the row

[[email protected] test]$ grep-c1--color=auto ' name ' T1file.txt
Who is?
My name is Brad Pitt.
Your Phone number is:135456789
[Email protected] test]$

There is no difference between the use of-a#,-b# and-c#, this is not tested here.

2.5) Test-e option: The line containing the string you or you

[Email protected] test]$ grep-e--color=auto ' (y| Y) ou ' t1file.txt
Who is ?
you r phone number is:135456789
[Email protected] test]$


3. Regular expressions

Regular expression, also known as: Regular expression,re. A concept in computer science that consists of a bunch of meta-characters,
In conjunction with human thought, a finite combination of metacharacters can be used to express a series of strings conforming to certain grammatical patterns, in text processing
Fields can be retrieved using other tools to replace the characters that match the pattern and display them in a predetermined format.

Regular expressions are divided into: basic regular expressions and extended regular expressions.

Basic regular expression in meta-character:

======= match characters, numbers and symbols =========

.: Represents any single character

[]: Represents any single character within the specified range, for example: [0-9] represents any single number from 0 to 9

[^]: represents any single character outside the specified range, for example: [^0-9] represents any single character that is not a number

[[:d Igit:]]: Any of 0 to 9 digits

[[: Lower:]]: Any of the lowercase letters

[[: Upper:]]: Any one of the uppercase letters

[[: Alpha:]]: Any one of the uppercase or lowercase letters

[[: Alnum:]]: Any one of the alphanumeric

[[:p UNCT:]]: Punctuation matches any one of the

[[: Space:]]: any whitespace character

======= matches the number of occurrences of a character ===========

\*: The left character appears any time

\?: The left character appears 0 or 1 times

\+: The left character appears at least 1 times

\{m,n\}: The character on the left appears m to n times

\{m,\}: The left character appears at least m times

\{0,n\}: The left character appears up to n times

======= Anchor Position =======================

^: Indicates that the right character appears at the beginning of the line

$: Indicates that the left character appears at the end of the line

\<: Indicates that the character on the right appears at the header of the word and can use \b instead

\>: Indicates that the character on the left appears at the end of the word and can use \b instead

\<word\>: Absolutely matches word's word

======== grouping and referencing ====================

\ (patten\): PATTEN appears as a whole

\ (p1\) ... \1....:\1 place a character identical to the front P1


Example:

3.1) line match symbol ^: Displays the row of root at the beginning of the/etc/passwd file

[[email protected] test]$ grep--color ' ^root '/etc/passwd
Root: X:0:0:root:/root:/bin/bash
[Email protected] test]$
3.2) line end match $: Displays the line in the/etc/passwd file where bash is at the end of the line

[[email protected] test]$ grep--color ' bash$ '/etc/passwd
root:x:0:0:root:/root:/bin/Bash
lijun:x:500:500:lijun,115,110,119:/home/lijun:/bin/Bash

[Email protected] test]$

3.3) Anchor word \< anchor suffix \>: Displays a line in the/etc/passwd file that contains words that begin with R, end with T, and 2 characters in the middle.

[[email protected] test]$ grep--color=auto ' \<r. T\> '/etc/passwd
root: x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[Email protected] test]$

3.4) display. Combined with * and greedy mode: Displays lines in the/etc/passwd file that contain words that begin with R, end with a T, and any character in the middle.

[[email protected] test]$ grep--color=auto ' \<r.*t\> '/etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[Email protected] test]$

The matching length in the first line of the display is due to the greedy pattern of the regular expression.

3.5) Use of the range symbol []: Displays lines starting with s in/proc/meminfo

[[email protected] tt]# grep--color=auto ' ^[ss] '/proc/meminfo
Swapcached:0 KB
Swaptotal:1063928 KB
Swapfree:1063928 KB
Shmem:172 KB
Slab:101220 KB
Sreclaimable:46408 KB
Sunreclaim:54812 KB
[Email protected] tt]# grep-i ' ^s '/proc/meminfo
swapcached:0 KB
swaptotal:1063928 KB
swapfree:1063928 KB
shmem:172 KB
slab:101228 KB
sreclaimable:46412 KB
sunreclaim:54816 KB

3.6) Grouping and referencing demo: Search for each line in the following document has a word that begins with L, and any character in the middle of e ending with a line that requires the same tail word at the beginning

[email protected] tt]# cat 3.txt
She's my love,isn ' t your Love
I like her leg,but she's Your Love
She isn't my love,but her phone as my
I like dog,but She-like cat

[[email protected] tt]# grep--color ' \ (\<l. e\>\). *\1 ' 3.txt
She's my love,isn ' t your love
I like dog,but she -like cat

The words in the row that are not the beginning of e are pattern. * Match cannot be removed


An extended regular expression:

Use an extended regular expression to use GREP-E or egrep


======= match characters, numbers and symbols =========

Same as the basic regular expression

======= Anchor Position =======================

Same as the basic regular expression

======= matches the number of occurrences of a character ===========

*: The characters on the left appear any time

?: The left character appears 0 or 1 times

+: The left character appears at least 1 times

{M,n}: The character on the left appears m to n times

{m,}: The character on the left appears at least m times

{0,n}: The character on the left appears up to n times

======== grouping and referencing ====================

(Patten): Patten appears as a whole

(P1) ... \1....:\1 the position of a P1 identical to the front

======== Select ======================

A|b: Indicates a or b

Example:

3.7) write a pattern to match the following e-mail addresses:

[email protected] tt]# cat 2.txt
[Email protected]
[Email protected]
[Email protected]
[Email protected] is j.com
This is my email:[email protected],and this is my HTTP address:http://www.sina.com


[Email protected] tt]# grep-i-e--color=auto ' \<[[:alnum:]]+ (-|_) {0,}[[:alnum:]]{0,}\>\@\<[[:alnum:]]+\ >\.com ' 2.txt
[Email protected]
[Email protected]
[Email protected]
[email protected] is j.com
This is my e-mail:[email protected], and this is my HTTP address:http://www.sina.com
[Email protected] tt]#

3.8) write a pattern to match the numbers that appear in the Ifconfig eth1:

[Email protected] tt]# ifconfig eth1|grep--color=auto-e ' [0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] '
Eth1Link encap:ethernet HWaddrxx:0C: in: the: on:8B
inet Addr:172. -. $.2Bcast:172. -. $.255Mask:255.255.255.0
inet6Addr:fe the:: -C: inFf:fe the: -b/ -Scope:link
Up broadcast RUNNING multicast MTU: theMetric:1
RX Packets:12535Errors0Dropped0Overruns:0Frame0
TX Packets:4685Errors0Dropped0Overruns:0Carrier0
Collisions:0txqueuelen:1000
RX bytes:1144613(1.0MiB) TX Bytes:569776(556.4KiB)
[Email protected] tt]#


3.9) write a pattern to match the IP address and mask that appears in the Ifconfig eth1

[Email protected] tt]# ifconfig eth1|grep--color=auto-e ' (\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \>\.) {3}\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> '
inet addr:172.16.200.2 bcast:172.16.200.255 Mask:255.255.255.0


Well, that's all, it's the regular expressions and grep usage that are commonly used, and in more detail please study through books, Internet and other inquiries.

Thanks to my wife for the Lollipop, let me support this writing finished this article.

----------------------------------Summary-----------------------------------------------

To finish these, the Fat Man glanced at the 3 companions here, looking at their hair drooped eyes, fat heart thought: This is better than the temptation to use? At the next feast, it's time to prepare some awk,sed with the use of regular expressions so that you don't have to pay for them, quack.

Then shouted to bartender, "The 3 seats on the seat to eat the bill." To get out of the way.


This article from "Elder brother is not pirates" blog, declined reprint!

Simple use of regular expressions on the Gnu/linux platform

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.