grep常见应用实例

前端之家收集整理的这篇文章主要介绍了grep常见应用实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在我们实际应用中,正则表达式的应用是非常广泛的,今天我就大家分享几个比较常见的正则表达式的应用实例,不周之处,还望高人多多指点!

一:使用正则表达式搜索邮箱地址

二:使用正则表达式搜索手机号

三:使用正则表达式搜索身份证号

四:使用正则表达式取网络配置文件中的IP地址的过程解析

一:使用正则表达式搜索邮箱地址:

  1. [root@centos7test]#catmail
  2.  
  3. 183530300@qq.com
  4.  
  5. netadmin@126.com
  6.  
  7. all-user@vip.qq.com
  8.  
  9. love_you@51cto.com
  10.  
  11. linuxedu@foxmail.com.cn
  12.  
  13. love-fire@vip.sina.com
  14.  
  15. love@vip.yahoo.com
  16.  
  17. ThisisamailBoxaddresstable[root@centos7test]#egrep"\<([[:alnum:]]+(-|_)*[[:alnum:]]*)\>@([[:lower:]]|[[:digit:]])+\.[[:lower:]]*(\.[[:lower:]]+)*"mail
  18.  
  19. 183530300@qq.com
  20.  
  21. netadmin@126.com
  22.  
  23. all-user@vip.qq.com
  24.  
  25. love_you@51cto.com
  26.  
  27. linuxedu@foxmail.com.cn
  28.  
  29. love-fire@vip.sina.com
  30.  
  31. love@vip.yahoo.com
  32.  
  33. ThisisamailBoxaddresstable
  34. qq_12345@qq.com
  35.  
  36. -NB74110@qq.com

wKioL1ekq4DhDmMcAAB_oEMww2g684.png

二:使用正则表达式搜索手机号:

  1. [root@centos7test]#cattel
  2.  
  3. 12812345678
  4.  
  5. a13012345678
  6.  
  7. b13112345678
  8.  
  9. c13212345678
  10.  
  11. 1331234567890
  12.  
  13. 13312345678
  14.  
  15. 13412345678
  16.  
  17. 13512345678
  18.  
  19. 13612345678
  20.  
  21. 13712345678
  22.  
  23. 13812345678
  24.  
  25. 13912345678
  26.  
  27. 14012345678
  28.  
  29. 14712345678
  30.  
  31. 15012345678
  32.  
  33. 15112345678
  34.  
  35. 15212345678
  36.  
  37. 15312345678
  38.  
  39. 15412345678
  40.  
  41. 15512345678
  42.  
  43. 15612345678
  44.  
  45. 15712345678
  46.  
  47. 15812345678
  48.  
  49. 15912345678
  50.  
  51. 16012345678
  52.  
  53. 17012345678
  54.  
  55. 17712345678
  56.  
  57. 17812345678
  58.  
  59. 18012345678
  60.  
  61. 18112345678
  62.  
  63. 18212345678
  64.  
  65. 18312345678
  66.  
  67. 18412345678
  68.  
  69. 18512345678
  70.  
  71. 18612345678
  72.  
  73. 18712345678
  74.  
  75. 18812345678
  76.  
  77. abc1891234567890def
  78.  
  79. 19012345678
  80.  
  81. [root@centos7test]#egrep"\<13[0-9]{9}|147[0-9]{8}|15([0-3]|[5-9])[0-9]{8}|17[0,7][0-9]{8}|18([0-3]|[5-9])[0-9]{8}\>"tel

搜索结果如图所示

wKioL1ekq6WxyoO_AACPMayo1zA106.png

三:使用正则表达式搜索身份证号:

  1. [root@centos7test]#catid
  2.  
  3. 123456789876543210
  4.  
  5. 130626199010016512def
  6.  
  7. 130626199010016512
  8.  
  9. abc130626199010016512
  10.  
  11. 160626199010016512
  12.  
  13. 200626199010016515
  14.  
  15. 220626199010016515
  16.  
  17. 240626199010016515
  18.  
  19. 30062619901001651X
  20.  
  21. 32062619901001651X
  22.  
  23. 38062619901001651X
  24.  
  25. 402062620080808100
  26.  
  27. 420626198002023518
  28.  
  29. 42062619901001651X
  30.  
  31. 462062620080808100
  32.  
  33. 472062620080808100
  34.  
  35. 500626199010016517
  36.  
  37. 522122199808081671
  38.  
  39. 520626199010016517
  40.  
  41. 550626199010016517
  42.  
  43. 560626199010016517
  44.  
  45. 600626199010016519
  46.  
  47. 650626199010016519
  48.  
  49. 660626199010016519
  50.  
  51. 70162619900231651X
  52.  
  53. 71162619901001651X
  54.  
  55. 720626199010016510
  56.  
  57. 800626199010016511
  58.  
  59. 987654328765432165
  60.  
  61. 810626199002316511
  62.  
  63. 810626201800016511
  64.  
  65. 820626199010016511
  66.  
  67. 820626101013326511
  68.  
  69. 830626100100165119
  70.  
  71. 584721520131499999
  72.  
  73. [root@centos7test]#egrep"\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|([0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>"id
  74.  
  75. 130626199010016512
  76.  
  77. 220626199010016515
  78.  
  79. 32062619901001651X
  80.  
  81. 420626198002023518
  82.  
  83. 42062619901001651X
  84.  
  85. 500626199010016517
  86.  
  87. 522122199808081671
  88.  
  89. 520626199010016517
  90.  
  91. 650626199010016519
  92.  
  93. 71162619901001651X
  94.  
  95. 810626199002316511
  96.  
  97. 820626199010016511

wKiom1elRhqTFUSNAABXMPOMfsk095.png

四:使用正则表达式取网络配置文件中的IP地址的过程解析:

取四段中的第一段

  1. [root@centos7test]#egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>."/etc/sysconfig/network-scripts/ifcfg-eno16777728
  2.  
  3. IPADDR=10.1.254.254
  4. GATEWAY=10.1.0.1
  5. DNS1=202.106.0.20

取四段中的第二段

  1. [root@centos7test]#egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.)"/etc/sysconfig/network-scripts/ifcfg-eno16777728
  2.  
  3. IPADDR=10.1.254.254
  4. GATEWAY=10.1.0.1
  5. DNS1=202.106.0.20

取四段中的第三段

  1. [root@centos7test]#egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){2}"/etc/sysconfig/network-scripts/ifcfg-eno16777728
  2.  
  3. IPADDR=10.1.254.254
  4. GATEWAY=10.1.0.1
  5. DNS1=202.106.0.20

取四段中的第四段

  1. [root@centos7test]#egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){2}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>"/etc/sysconfig/network-scripts/ifcfg-eno16777728
  2.  
  3. IPADDR=10.1.254.254
  4. GATEWAY=10.1.0.1
  5. DNS1=202.106.0.20

显示IP地址

  1. [root@centos7test]#egrep-o"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){2}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>"/etc/sysconfig/network-scripts/ifcfg-eno16777728
  2.  
  3. 10.1.254.254
  4. 10.1.0.1
  5. 202.106.0.20

wKioL1ekJZzS12d7AACxvie2GK0469.png

猜你在找的正则表达式相关文章