Regular expressions and grep, sed tools

Source: Internet
Author: User

What are regular expressions

A method of handling characters that can be used to handle regular expression strings as long as the command tool (e.g. grep, sed, awk, and so on) supports this method. With the help of special characters, we can easily find, delete, replace the specific string of the command program.

Use

Information filtering, information matching, access to useful information

Common expression Meanings

  • ^ line marks #^haha, matches lines starting with haha

  • $ line Tail Mark

  • . Match any single character

  • ? Match the previous item once or 0 times #blu? e matches blue or ble

  • + Match Previous item one or more times

  • * Match Previous Items 0 or more times

  • [] matches any one of the characters contained in [] #a [bc]d matches Abd or ACD

  • [^] matches any character except [^] #a [^BC] matches ad, AE ...

  • [-] match [-] any one of the characters #[1-9] matches 1-9 any one character

  • () Create a substring #ab (CDE) for matching? Match AB or ABCDE

  • {n} matches the previous item n times #[1-9]{3} equivalent to [1-9][1-9][1-9]

  • {N,} items that match at least n times

  • Item before {n,m} matches n-m times

  • | Match any one of the two sides # (a | b) match A or b

  • \ Escape

  • [: Alnum:] Uppercase and lowercase English and numerals

  • [: Alpha:] case in English

  • [:d Igit:] Number

  • [: Lower:] lowercase English

  • [: Upper:] Capital English

  • [: Blank:] tab or Space

  • [:p UNCT:] Punctuation

Grep

grep [-N line number ] [-v inverse selection ] [-A several lines after output] [-B Output first few lines] [-C several lines before and after output] [-Iignores case ] [-O printing matches to the entire line ] [-B matched to character offset] [-L Search matches text in that file] [-L and-l opposite] [-e matches multiple ] [-R or-R recursive search, can specify include and exclude] [-Q Silent output successfully returned 0] [--color=auto Color Highlight]

    • For an extended regular expression +? | () need to use GREP-E or egrep

    • Example: Intercept DMESG eth0 information, highlight, and display the first two lines after three lines, display line number

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/C8/wKiom1WHfnmSNAwHAAGiMzd6dNc671.jpg "style=" float: none; "title=" 2015-06-22_10-53-55.png "alt=" Wkiom1whfnmsnawhaagimzd6dnc671.jpg "/>

    • Parameter-O

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/C5/wKioL1WHgDDjrSfqAAFx37DsG7Y029.jpg "title=" Image.png "style=" Float:none; "alt=" wkiol1whgddjrsfqaafx37dsg7y029.jpg "/>

    • Parameter-B

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/C8/wKiom1WHfnnjx9f-AAB_5lR8X54885.jpg "style=" float: none; "title=" Image2.png "alt=" Wkiom1whfnnjx9f-aab_5lr8x54885.jpg "/>

    • Parameters-L and L

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/C5/wKioL1WHgDCgi094AAB_G4973cc998.jpg "style=" float: none; "title=" Image3.png "alt=" Wkiol1whgdcgi094aab_g4973cc998.jpg "/>

    • Parameter-E

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/C9/wKiom1WHfnqhfzLUAABWraLUgxs389.jpg "style=" float: none; "title=" Image6.png "alt=" Wkiom1whfnqhfzluaabwralugxs389.jpg "/>

    • Parameters-R and-R

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/C9/wKiom1WHfnniIbJpAACj3w02MH0980.jpg "title=" Image4.png "style=" Float:none; "alt=" wkiom1whfnniibjpaacj3w02mh0980.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/C5/wKioL1WHgDDzLNW6AABPpI1W9dM744.jpg "style=" float: none; "title=" Image5.png "alt=" Wkiol1whgddzlnw6aabppi1w9dm744.jpg "/>


PS: Sometimes because of the language of different languages, the use of such expressions like [A-z] there will be errors, different encoding sequence, such as Zh_cn.big5 and C two languages

    • Lang=c: 01234 .... Abcd.. Yzabcd....xyz

    • Lang=zh_cn:01234....aabbcc....zz

Available [: Lower:] instead

Sed: Stream Editor Stream editors

Data can be inserted, deleted, replaced and printed onto the screen, or directly written, with partial data lookup replacements, matched string markers, and substring matching tokens

sed [-I direct write] [-e continuous Operation] [-n silent mode] ' operation content '

1: Add a,i delete D replace C display P,-n//(according to line number)

    • [Email protected] ~]# NL/ETC/PASSWD | sed ' 2a hello '///second line added below

1 Root:x:0:0:root:/root:/bin/bash
2 Bin:x:1:1:bin:/bin:/sbin/nologin
Hello
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin

    • [Email protected] ~]# NL/ETC/PASSWD | sed ' 2i hello '//second line above added

1 Root:x:0:0:root:/root:/bin/bash
Hello
2 Bin:x:1:1:bin:/bin:/sbin/nologin

    • D

    • [Email protected] ~]# NL/ETC/PASSWD | sed ' 2,4d '//delete 2-4 lines

1 Root:x:0:0:root:/root:/bin/bash
5 Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

    • [Email protected] ~]# NL/ETC/PASSWD | sed ' 2,5c hello '//2–5 line swap

1 Root:x:0:0:root:/root:/bin/bash
Hello
6 Sync:x:5:0:sync:/sbin:/bin/sync

    • [Email protected] ~]# NL/ETC/PASSWD | sed-n ' 2,4p '//display 2-4 rows,-N (quiet mode) No output difference is very large

2 Bin:x:1:1:bin:/bin:/sbin/nologin
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 Adm:x:3:4:adm:/var/adm:/sbin/nologin

2: Partial Data Lookup replacement

sed ' s/to replace field/new field/g ' # ' S///ng ' the nth start of each line to replace

Example: Intercept the IP address of the eth0

    • [Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | sed ' s/^.*addr://g ' | sed ' s/bcast.*$//g '
      192.168.159.131

combined with regular expressions

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/C5/wKioL1WHrDqxJhJvAABWhGXl8cw859.jpg "style=" float: none; "title=" Image.png "alt=" Wkiol1whrdqxjhjvaabwhgxl8cw859.jpg "/>

3: Matched string tag &

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/C9/wKiom1WHqoTS8rLEAACW87fy8a0089.jpg "style=" float: none; "title=" Image2.png "alt=" Wkiom1whqots8rleaacw87fy8a0089.jpg "/>

4: String match tag \1\2

Format: ' s/() ()/\1 \2 '

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/C5/wKioL1WHrDvCIOiXAABZYJ-sjco421.jpg "style=" float: none; "title=" Image3.png "alt=" Wkiol1whrdvcioixaabzyj-sjco421.jpg "/>


This article is from the "Call Me boxin" blog, so be sure to keep this source http://boxinknown.blog.51cto.com/10435935/1664098

Regular expressions and grep, sed 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.