Linux Extended Regular Expressions

Source: Internet
Author: User
Tags egrep

1. Extended Regular Expression 1.1 + (plus sign)

+ indicates that the previous character appears 1 or more times or more than 1 times

1.1.1 Understanding +

Requirements: Remove the file contents of the continuous occurrence of lowercase letters

[[email protected]/oldboy]#egrep ' 0+ ' oldboy.txt grep-e===egrepmy QQ is49000448 not4900000448. [[Email protected]-lnb/oldboy]#grep-e ' 0+ ' oldboy.txtmy QQ is49000448 not4900000448. [[Email protected]-lnb/oldboy]#grep ' 0\+ ' oldboy.txtmy QQ is49000448 not4900000448. [[Email protected]-50 Oldboy]#egrep ' [a-z]+ ' Oldboy.txtyou can use [a-z]+Show too much word content here do not paste [[email protected]-50 Oldboy]#egrep ' [a-z]+ ' Oldboy.txt-o-o display the lookup processAmoldboy##[] with + + will think [] inside is a whole so display as a character group

The difference between 1.1.2 Plus + and no plus +
[[email protected]/oldboy] # egrep ' 0+ ' oldboy.txt-o    Plus + sign together will show 00000000[[email protected]-lnb/oldboy]# egrep ' 0 ' oldboy.txt-o     no + + number alone display 00000000
View Code1.1 | or 1.1.1 Understanding |
[[email protected] Oldboy] #  egrep ' oldboy|linux ' oldboy.txt  find the contents of Oldboy or Linux in the file is HTTP  //Is http://blog.oldboyedu.com

1.1.2 | Usage of

Exclude # and Blank lines in a file

/etc/ssh/sshd_config exclude # and Blank lines in a file

[[email protected] ssh] # egrep-v ' ^$|# ' sshd_config
1.1 () (parentheses)

() represents a whole reverse reference/backward reference

Sed-r using the extended regular

Idea: Protect what you want ()

SED uses a \ Numeric reference between two well numbers after ' s## #g '

[[email protected] Oldboy]#egrep ' oldb (o|e) y ' oldboy.txt here means finding Oldboy and Oldbey[[Email protected]-50 Oldboy]#echo 123456|sed-r ' s# (. *) #\1#g '123456[[Email protected]-50 Oldboy]#echo 123456|sed-r ' s# (. *) #<\1> #g '<123456>Change 123456 to<34>56[[Email protected]-50 Oldboy]#echo 123456|sed-r ' s# (#<\1> #g ')12<34>56the dot represents any one character, and \2 represents the second parenthesis. [[Email protected]~]#echo 123456|sed-r ' s# (.). (..). (.) #\2#g '34
1.1 {} (curly braces)

The previous character has at least consecutive n times, with a maximum of M occurrences

Different methods of 1.1.1 curly braces
0{3,6}   >=3 <=60{3}     ==30{3,}    >=30{,6}    >=0 <=6
1.1.2 Application Method
[[email protected] Oldboy] # egrep ' [0-9x]{18} ' ID.txt     here means {} The preceding characters appear 18 times [[email protected]-lnb/oldboy]#  Egrep ' 0{3,4} ' oldboy.txt indicates that 0 appears with a minimum of 3 occurrences up to 4 times   is 49000448 not 4900000448 . [Email protected] -lnb/oldboy]#  egrep ' 0{3} ' oldboy.txt   means 0 has appeared 3 times   was 49000448 not 4900000448.
Usage of 1.1.3 {}

Remove the ID number from the file

[[email protected]/oldboy]#Cat ID.txtGold211324198705244720million500224197105168312ren 1231231231oldboy any 3oldboy ren lidao97303136098 ren alex2197303136098 ren 350182197303oldgir lv211282199209113038Hole150000198309176071Zou371001197412221284He130185200011215926du362522198711278101to 14052219961008852x[[email protected]-lnb/oldboy]#egrep ' [0-9x]{18} ' ID.txt#The expression 0 to 9 and X appeared altogether 18 times such that the lookup was inaccurate because a single number appeared 18 times can also be filtered outGold211324198705244720million500224197105168312LV211282199209113038Hole150000198309176071Zou371001197412221284He130185200011215926du362522198711278101xxxxxxxxxxxxxxxxxx to 14052219961008852x[[email protected]-lnb/oldboy]#egrep ' [0-9]{17}[0-9x] ' id.txt exact search but not the most accurateGold211324198705244720million500224197105168312LV211282199209113038Hole150000198309176071Zou371001197412221284He130185200011215926du362522198711278101to 14052219961008852X
View Code1.1? Question mark

? The previous symbol appears consecutively 0 or 1 times with or without

? The information represented by 0 or 1 times represents the same content as {0,1} is actually the same

1th Chapter Regular Expression Pit 1.1 Pit 1 grep ' 0* '

will show the contents of the file.

0* 0 times No grep will show the contents of the file.

0 times or more 0 00000000000

 [[email protected] ~]#   grep "0*" Clsn.txt Pit 1 grep ' 0* ' will display the contents of the file  i am clsn teacher! I Teach Linux. I like badminton ball, billiard ball  and   Chinese chess!my blog  is  http:// clsn.blog.51cto.com our site  is  http:// www.etiantian.org my qq num  is  49000448.  not  4900000448.my God, I am  not   Oldbey,but clsn![ [Email protected]  ~]#   Grep-o "0*" Clsn.txt  00000000 
View Code

1.2 Pit 2 [Oldboy]

[Oldboy] looks for o l d b o y instead of Oldboy overall

[[email protected]/oldboy]#grep ' [Oldboy] ' Oldboy.txtWhat Oldboy find here is o l d b o y instead of Oldboy overall I am oldboy teacher! I Teach Linux. I like badminton ball, billiard Ball andChinese Chess!my Blog is/HTTPoldboy.blog.51cto.comour Size is/HTTPBlog.oldboyedu.commy QQ is49000448 not4900000448. My God, I am notoldbey,but oldboy! [[Email protected]-lnb/oldboy]#grep-o ' [Oldboy] ' oldboy.txt display the Find processoldboyllb
View Code

1.3 Pit 3 [^#^$] [^$] [^#$]

[] in $. No special meaning similar to \ escape character

[[email protected]/oldboy]#cat Oldboy.txt viewing file contentsI am Oldboy teacher! I Teach Linux. I like badminton ball, billiard Ball andChinese chess!+++++\\\\\\!!!! $$$$$$^^^####!!! ^^ ^^ ^[email protected]@@@@####[[Email protected]-lnb/oldboy]#grep ' [^#^$] ' oldboy.txt here means to exclude #^$ equivalent to removing the special meaningI am Oldboy teacher! I Teach Linux. I like badminton ball, billiard Ball andChinese chess!+++++\\\\\\!!!! $$$$$$^^^####!!! ^^ ^^ ^[email protected]@@@@####[[Email protected]-lnb/oldboy]#grep ' \.$ ' oldboy.txt here means to find the end of the.I teach Linux. not4900000448. [[Email protected]-lnb/oldboy]#grep ' [.] $ ' oldboy.txt here also means to find the same as the. EndI teach Linux. not4900000448.
View Code

1.4 Pit 4 grep-v with [^a-z]

[^a-z] excludes certain characters by character

Grep-v [A-z] excluding row by line

[[email protected]/oldboy] # grep ' [^a-z] '  oldboy.txt[[email protected]-lnb/oldboy]#  grep-v ' [A-z] '  Oldboy.txt

Linux Extended Regular Expressions

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.