Use of regular expressions
1.1 Creating an environment
Cat >q.txt<<eof
Oldboy Oldboy
Hellooldboy Hellooldboy
Awkoldboy Awkoldboy
Sedoldboy Sedoldboy
oldboyedu oldboyedu
Eof
Take out only the Oldboy line
grep "\boldboy\b" Q.txt
Instance 1-1 Reverse Reference
[Email protected]~]# # # # #第一个里程碑-Reverse reference (first protected and then used)
[Email protected]~]# echo 123456|sed-r ' s#. (..).. #\1#g '
34
[Email protected]/]# echo 123456|sed-r ' s# (.). (..). (.) #\1\2\3#g '
1346
1. * 0 or more times + 1 or more times? 0 or 1 times {} Customize several or several times to several times
1.2 Summary of regular expressions
Egrep/grep understand, simple look effect, result.
Egrep/grep-o the parameters to see exactly what the regular match is.
With Grep,egrep,sed-r,awk more powerful.
1.3 References to regular expressions
Mans grep
Infogrep
Mans 7 Glob
Linux Regular expression syntax
http://aresxin.blog.51.cto.com/4734097/1602624
Regular Expressions 30-minute introductory tutorial
Http://deerchao.net/tutorizls/regex/regex.htm.htm#mission
The use of 1.4 sed in regular expressions
Example 1-2 removing 644 or 0644 in/etc/hosts
Enter the environment: [[email protected] ~]# stat/etc/hosts
# # #方法一
[Email protected] ~]# stat/etc/hosts
[Email protected] ~]# Stat/etc/hosts|awk ' nr==4 ' |sed ' s#^.* (0# #g ' |sed ' s#/.*$# #g '
644
Method Two:
[[email protected] ~]# Stat/etc/hosts|awk ' nr==4 ' |sed-r ' s#^.*\ (0 (. *)/-.*$#\1#g '
644
# # #方法三 Awk
[Email protected] ~]# stat/etc/hosts
[[Email protected]~]# stat/etc/hosts |awk ' nr==4 ' |awk-f ' [0/] ' ' {print $} '
644
1.5 sed Reverse Reference remove/etc/hosts file permission plot
Example 1-3
# #找出oldboy a line in. txt that begins with M or N and ends with. or M
[Email protected]~]# cat Oldboy.txt
[[Email protected]~]# # # #第一个里程碑-M or N
[[email protected] ~]# grep "^[mn]" oldboy.txt
[Email protected]~]# # # #第二个里程碑-Find the end of. or M
[[email protected] ~]# grep "[. m]$" Oldboy.txt
[[Email protected]~]# ####[] There is no special meaning to the symbol special symbol in the expression.
[Email protected]~]# grep ". $" Oldboy.txt
[Email protected]~]# # # # #第三个里程碑-and
[[email protected] ~]# grep "^[mn].*[.m]$" Oldboy.txt
1.6 Remove the IP address and 10.0.0.255 of the network card
Example 1-4
[Email protected] ~]# #取出网卡的ip地址和10.0.0.255
[Email protected] ~]# ifconfig eth0
[Email protected] ~]# ifconfig eth0 |sed-n ' 2p '
[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |sed-r ' S#^.*DR: (. *) Bc.*t: (. *) M.*$#\1#g '
10.0.0.200
[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |sed-r ' S#^.*DR: (. *) Bc.*t: (. *) M.*$#\2#g '
10.0.0.255
[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |sed-r ' S#^.*DR: (. *) Bc.*t: (. *) M.*$#\1\2#g '
10.0.0.20010.0.0.255
[[email protected] ~]# ifconfig eth0 |sed-n ' 2p ' |sed-r ' S#^.*DR: (. *) Bc.*t: (. *) m.*$#\1 \2#g '
10.0.0.200
1.7 Use awk to remove the IP address and 10.0.0.255 of the network card
Method Three Awk method
[Email protected] ~]# ifconfig Eth0|awk ' nr==2 '
[[email protected] ~]# ifconfig eth0|awk ' nr==2 ' |awk-f "[:]" ' {Print $13} '
10.0.0.200
[Email protected]~]# ifconfig eth0|awk ' nr==2 ' |awk-f ' [:]+ ' ' {print $4} '
10.0.0.200
10.0.0.255
[Email protected]~]# ifconfig eth0|awk-f "[:]+" ' Nr==2{print $4,$6} '
10.0.0.20010.0.0.255
Method Four
Remove IP address and 255.255.255.0
[Email protected] ~]# ifconfig eth0 |awk ' nr==2 ' |awk-f "[:]+" ' {print $4, $NF} '
10.0.0.200
255.255.255.0
Method Five
Take IP address permissions
[Email protected] ~]# ifconfig eth0
[[email protected] ~]# ifconfig eth0 |awk-f "addr: ' Nr==2{print $} '
[[email protected] ~]# ifconfig eth0 |awk-f "Addr:|bcast: ' Nr==2{print $} '
10.0.0.200
1.8 + Usage
Re-recognize the next + consecutive occurrences
Example 1-5
Usage One
[Email protected] ~]# echo "##########[email protected]@@@@@@@@@2"
##########[email protected]@@@@@@@@@2
[Email protected] ~]# echo "##########[email protected]@@@@@@@@@2" |grep "[#@]"
##########[email protected]@@@@@@@@@2
[Email protected] ~]# echo "##########[email protected]@@@@@@@@@2" |grep "[#@]"-O
#
Usage Two
[Email protected] ~]# echo "##########[email protected]@@@@@@@@@2" |egrep "[#@]+]
##########[email protected]@@@@@@@@@2
[[email protected] ~]# echo "##########[email protected]@@@@@@@@@2" |egrep "[#@]+"-O
##########
@@@@@@@@@@
[Email protected] ~]#
[Email protected] ~]# echo "##@@@@@@###@@@@#####[email protected]@@###@@@@@@@2" |egrep "[#@]+]
##@@@@@@###@@@@#####[email protected]@@###@@@@@@@2
[Email protected] ~]# echo "##@@@@@@########[email protected]@@###@@@@@@@2" |egrep "[#@]+]
##@@@@@@########[email protected]@@###@@@@@@@2
[[email protected] ~]# echo "##@@@@@@########[email protected]@@###@@@@@@@2" |egrep "[#@]+"-O
##@@@@@@########
@@@###@@@@@@@
1.9 [] use in regular expressions
###[] Regular expressions will assume that the contents of the brackets are the same ABC
# # #1. In my basket
# # #2. Not in my basket.
Example 1-6
[[email protected] ~]# echo "##########[email protected]@@@@@@@@@2" |awk-f "[#@]+" ' {print $} '
1
[Email protected] ~]# ifconfig eth0 |awk ' nr==2 ' |sed ' s#:# #g '
Use of 1.10 cut
Method Four-cut
# #先把冒号替换为空格取第13列
Example 1-7
[Email protected] ~]# ifconfig eth0 |awk ' nr==2 ' |sed ' s#:# #g '
[Email protected] ~]# ifconfig eth0 |awk ' nr==2 ' |sed ' s#:# #g ' |cut-d ' "-f13
10.0.0.200
# #先通过: Split, then split by space
[Email protected] ~]# ifconfig eth0 |awk ' nr==2 ' |cut-d ":"-f2
10.0.0.200
Bcast
[Email protected] ~]# ifconfig eth0 |awk ' nr==2 ' |cut-d ":"-f2|cut-d ""-f1
10.0.0.200
This article is from the "Heyong" blog, make sure to keep this source http://heyong.blog.51cto.com/13121269/1954911
Use of Linux Regular expressions