Cigarette smoker---------Text processing of Linux Egre and fgrep of Three Musketeers

Source: Internet
Author: User
Tags expression engine egrep

Egrep is another mode of grep, with the-e option (enabling or shutting down one or more of the commands), enabling the extended Regular expression engine feature to use patterns that extend the regular expression character and the combination of plain text characters , A line-by-line check of text flow that displays the row of the matched string to the standard output

The FGREP,GREP-F option, which turns off the regular expression engine feature, combines plain text characters withpattern, based on a unique algorithm, checks the text for efficient matching , and matches the line where the string is located display to standard output


Egrep command

[[Email protected] ~]# type Egrepegrep is aliased to ' egrep--color=auto ' [[email protected] ~]# which--skip-alias egrep/u Sr/bin/egrep[[email protected] ~]# egrep--help #获取使用帮助Usage: grep [OPTION] ... PATTERN [File]...-e,--extended-regexp #ERE # Its helper content with the use of the grep command


-e option, grep-e equivalent to Egrep

# grep-e ' ^root\b '/etc/passwd# egrep ' ^root\b '/etc/passwd

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9D/6C/wKiom1l_-4nSQvzuAAASKk9Gtj0791.png "title=" grep- E.png "alt=" Wkiom1l_-4nsqvzuaaaskk9gtj0791.png "/>


Extending Regular Expression characters

Character matching

Number of Matches

Position anchoring

Or


Character matching

1). Any single character

2) [] any single character within the specified range, [ABC] A or B or C

3) [^] any single character outside the specified range


Using the example

[[email protected] ~]# vim a.txt #vim是一个文本编辑命令, enter after pressing I key, before writing text, after writing finished. Press the ESC key, and then press SHIFT +: to enter the Wq plus enter key. How is you?hwo old is you? How is it? HWO old is You?root:x:0:0:root:/root:/bin/bash

. Match any single character

# egrep ' R.. T ' a.txt

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9D/6B/wKiom1l_9XPBxNdCAAAII9Gqxjs786.png "title=" matches any single character. PNG "alt=" Wkiom1l_9xpbxndcaaaii9gqxjs786.png "/>

[] matches any single character within the specified range, [ABC] A or B or C

# egrep ' [EFH] ' a.txt

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9D/6B/wKioL1l_9jTRilLBAAANEkuiAm0800.png "title=" Matches any single character within the specified range. PNG "alt=" Wkiol1l_9jtrillbaaanekuiam0800.png "/>

[^] denotes any single character outside the specified range, [^ABC] not A and B and C

# egrep ' [^efh] ' a.txt

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/6B/wKioL1l_9qHhcXYrAAAP5fCxW4w214.png "title=" Any single character outside the range. PNG "alt=" Wkiol1l_9qhhcxyraaap5fcxw4w214.png "/>


Number of matches Number of occurrences of the preceding single character

1) * matches the preceding single character, appears 0, 1, or more

2)? Matches the preceding single character, 0 or 1 times

3) + matches the previous single character, appears at least 1 times, >=1 times

4) {m} exactly matches the preceding single character m times

5) {m,} matches the preceding single character, at least m times

6) {, n} up to n times

7) {M,n} at least m times, up to N times


Using the example

[Email protected] ~]# vim Output_delimiter.txtabcba12baababbabababababababab


* matches the preceding single character, appearing 0, 1, or more

# egrep ' a*b ' output_delimiter.txt

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9D/6C/wKioL1l_-cGQr_jrAAAN5cuhiEM442.png "title=" any time. PNG "alt=" Wkiol1l_-cgqr_jraaan5cuhiem442.png "/>


. * match. Any single character any time, any character of any length , (* in Glob)

# egrep ' r.*t ' a.txt

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/6C/wKiom1l_-riRcRSaAAAIXL63Q6c275.png "title=" Any character of any length. PNG "alt=" Wkiom1l_-rircrsaaaaixl63q6c275.png "/>


? matches the preceding single character, 0 or 1 times ( in the basic regular expression \? )

# egrep ' a?b ' output_delimiter.txt # grep ' a\?b ' output_delimiter.txt

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9D/6C/wKioL1l__DLDa2I4AAAOP4Ub1Eo129.png "title="?. PNG "alt=" Wkiol1l__dlda2i4aaaop4ub1eo129.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/6C/wKioL1l__MDSLdFlAAAOcDSorS4432.png "title=" Basic. PNG "alt=" Wkiol1l__mdsldflaaaocdsors4432.png "/>


+ matches the preceding single character, appearing at least 1 times, >=1 times ( \+ in the basic regular expression )

# egrep ' a+b ' output_delimiter.txt# grep ' a\+b ' output_delimiter.txt

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9D/6C/wKioL1l__R_DbUTWAAAL9nJFM_U113.png "title=" at least 1 times. PNG "alt=" Wkiol1l__r_dbutwaaal9njfm_u113.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9D/6C/wKiom1l__VCRMo_pAAAMHGZ4GmA580.png "title=" At least 1 times grep. PNG "alt=" Wkiom1l__vcrmo_paaamhgz4gma580.png "/>


{m} exactly matches the preceding single character m times ( \{m\} in the basic regular expression )

# egrep ' a{3}b ' output_delimiter.txt# grep ' a\{3\}b ' output_delimiter.txt

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/6C/wKioL1l__euSEVCIAAAJy3qyw7M183.png "title=" 3UQW. PNG "alt=" Wkiol1l__eusevciaaajy3qyw7m183.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9D/6C/wKiom1l__g3gLEtcAAAKIv1jrDs324.png "title=" 3 times. PNG "alt=" Wkiom1l__g3gletcaaakiv1jrds324.png "/>


{m,} matches the preceding single character, at least m times ( \{m,\} in the basic regular expression )

# egrep ' a{3,}b ' output_delimiter.txt# grep ' a\{3,\}b ' output_delimiter.txt

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9D/6C/wKioL1l__pWjaf3GAAAJsFepLwQ209.png "title=" at least 3 times. PNG "alt=" Wkiol1l__pwjaf3gaaajsfeplwq209.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9D/6C/wKiom1l__qazqraWAAAKMyMLfvk260.png "title=" Gcfit3 times. PNG "alt=" Wkiom1l__qazqrawaaakmymlfvk260.png "/>


{, n} matches the preceding single character up to n times ( \{m,\} in the basic regular expression )

# egrep ' a{,2}b ' output_delimiter.txt# grep ' a\{,2\}b ' output_delimiter.txt

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/6C/wKiom1l__9HxtL69AAAPF6-VNYg631.png "title=" up to 2. PNG "alt=" Wkiom1l__9hxtl69aaapf6-vnyg631.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/6C/wKioL1l__2rhls23AAAPRHJw3ZU952.png "title=" up to 3 times. PNG "alt=" Wkiol1l__2rhls23aaaprhjw3zu952.png "/>


{m,n} matches the preceding single character at least m times, up to N times ( \{m,n\} in the base regular expression )

# egrep ' a{3,6}b ' output_delimiter.txt# grep ' a\{3,6\}b ' output_delimiter.txt

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/6C/wKiom1mAAGfjxENaAAAT-AQJe5M387.png "title=" At least 3 to 6. PNG "alt=" Wkiom1maagfjxenaaaat-aqje5m387.png "/>


Position Anchor ' expected matching character must appear in a certain location '

1) ^ The beginning of the line anchor, used for the leftmost mode. by The string character that the regular expression matches to must appear at the beginning of the line

2) $ line end anchor for the right side of the pattern. A string character that is matched by a regular expression must appear at the end of the line

3) ^pattern$: The entire line can only match this mode

4) Match blank line: ^[[:space:]]*$ blank can have any number of times

5) \< or \b The first anchor for the left side of the word pattern

6) \> or \b ending anchor for the right side of the word pattern

7) \<pattern\> or \bpattern\b matches the entire word for the left and right sides of the word

8) grouping, back reference ()

^ The beginning of the line is anchored to the left of the pattern. by The string character that the regular expression matches to must appear at the beginning of the line

# grep-e ' ^root '/etc/passwd

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/6D/wKioL1mABmvyjIy1AAAQrKtD4Ss937.png "title=" anchors the first word. PNG "alt=" Wkiol1mabmvyjiy1aaaqrktd4ss937.png "/>



$ line-tail anchor for the right-most mode. A string character that is matched by a regular expression must appear at the end of the line

# grep-e '/bin/bash$ '/etc/passwd

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/6D/wKioL1mABsTjvcDyAABNVhnmns4443.png "title=" anchors the ending. PNG "alt=" Wkiol1mabstjvcdyaabnvhnmns4443.png "/>


^pattern$: The entire row can only match this pattern

# GREP-E-C ' ^$ '/etc/init.d/functions #匹配空行-C Displays the number of rows for all rows that match the string


Match blank line: ^[[:space:]]*$ blank can have any number of times

# GREP-E-C ' ^[[:space:]]*$ '/etc/init.d/functions #匹配可为空行或有空白字符的行


\< or \b The first anchor for the left side of the word pattern

# grep-e ' \broot '/etc/passwd


\> or \b Ending anchor for the right side of the word pattern

# grep-e ' root\b '/etc/passwd

\<pattern\> or \bpattern\b matches the entire word for the left and right sides of the word

# grep-e ' \broot\b '/etc/passwd


() grouping, ( the basic regular expression in \ (\) ) will be any character currently the same component

# Cat Grep.txt Abxyxxxxxxyxyxyxyxyabcxy
# grep-e ' (XY) + ' Grep.txt

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9D/6D/wKiom1mABaHhtcfrAAAJZiiJ5ys160.png "title=" Egre Group. PNG "alt=" Wkiom1mabahhtcfraaajziij5ys160.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/6D/wKioL1mABcCgwK04AAAJvgkac4g213.png "title=" grouping. PNG "alt=" Wkiol1mabccgwk04aaajvgkac4g213.png "/>


Attention:

When representing the string itself , if the command used by the regular expression character, there is \, then no \; If there is no \, then add \.

# Grep-o ' [[: alpha:]_]\+ () '/etc/rc.d/init.d/functions #正则表达式字符为 \ (\)
GREP-E-O ' [[: Alpha:]_]+\ (\) '/etc/rc.d/init.d/functions #扩展正则表达式字符为 ()


A back reference, the contents of the pattern in the grouping brackets are recorded in internal variables by the regular expression engine , and are named as: \1,\2,\3

# grep-e ' (^[[:alnum:]]+\>). *\1$ '/etc/passwd

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9D/6D/wKiom1mACGXgrMPuAAAVp_KTtEQ359.png "title=" With the shell. PNG "alt=" Wkiom1macgxgrmpuaaavp_ktteq359.png "/>


Or

[Ccat] C or C or a or T

c| Cat C or Cat

(c| C) at cat or cat

# Vim Dsdci.txtcatcatc12c123at

1, [Ccat] matches any single character within the specified range

# egrep ' [Ccat] ' dsdcI.txt

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9D/6D/wKioL1mACx2A4xSxAAAKCszkeZc503.png "title=" or. png "alt=" Wkiol1macx2a4xsxaaakcszkezc503.png "/>

2, c| Cat matches C or cat

# egrep ' c| Cat ' DsdcI.txt

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9D/6D/wKiom1mAC2jCxlKbAAAIjdA3gP0001.png "title=" or ... PNG "alt=" Wkiom1mac2jcxlkbaaaijda3gp0001.png "/>

3, (c| C) at match cat or cat

# egrep ' (c| C) at ' DsdcI.txt

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9D/6D/wKioL1mAC77hl50wAAAH_aidxsA632.png "title=" AKG ... PNG "alt=" Wkiol1mac77hl50waaah_aidxsa632.png "/>


This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1952624

Cigarette smoker---------Text processing of Linux Egre and fgrep of Three Musketeers

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.