[关闭]
@mrz1 2018-01-01T14:14:14.000000Z 字数 5072 阅读 822

linux-正则

笔记


主要内容

  • linux-正则

简介

  • Linux:文本处理三剑客
  • grep:文本过滤(模式:pattern)工具;grep, egrep, fgrep(不支持正则表达式搜索)
  • sed: stream editor,文本编辑工具
  • awk: Linux上的实现gawk,文本报告生成器

grep命令: Global search REgular expression and Print outthe line
作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查;打印匹配的行
模式:由正则表达式字符及文本字符所编写的过滤条件
Contos6中:grep没颜色 .bashrc 加上alias grep=’grep --color=auto’

grep命令

  1. grep [OPTIONS] PATTERN [FILE...]
  2. --color=auto: 对匹配到的文本着色显示 contos6手动添加
  3. -v: 显示不被pattern匹配到的行(取反)
  4. -i: 忽略字符大小写
  5. -n 显示匹配的行号
  6. -c: 统计匹配的行数
  7. -o: 仅显示匹配到的字符串
  8. -q: 静默模式,不输出任何信息(后期在详解)
  9. -A #: after, 后#行
  10. -B #: before, 前#行
  11. -C #: context, 前后各#行
  12. -e:实现多个选项间的逻辑or关系
  13. grep e cat -e dog file
  14. -w:匹配整个单词(数字,字母,下划线不能作为分隔符)
  15. -E:使用ERE
  16. -F:相当于fgrep,不支持正则表达式
  1. [root@centos6 ~]#alias grep //alias grep='grep --color=auto'
  2. [root@centos7 ~]#rpm -i /misc/cd/Packages/nmap-6.40-7.el7.x86_64.rpm //安装扫描系统漏洞
  3. [root@centos7 ~]#nmap -v -sP 172.18.0.0/24 //扫描网段
  4. [root@centos7 ~]#nmap -v -sP 172.18.0.0/24|grep -B1 up|grep "Nmap scan"|cut -d " " -f5
  5. grep root /etc/passwd
  6. grep "$USER" /etc/passwd
  7. grep '$USER' /etc/passwd //不行单引号是字符串
  8. grep `whoami` /etc/passwd

REGEXP

REGEXP:由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符字面意义,而表示控制或通配的功能
程序支持: grep,sed,awk,vim, less,nginx,varnish等
分两类:
基本正则表达式: BRE
扩展正则表达式: ERE
grep -E;egrep
正则表达式引擎:
采用不同算法,检查处理正则表达式的软件模块
PCRE(Perl(语言很牛掰和diff发明同一人) Compatible Regular Expressions)
元字符分类:字符匹配、匹配次数、位置锚定、分组
man 7 regex 查看详细信息

正则表达式元字符

  1. . 匹配任意单个字符
  2. [] 匹配指定范围内的任意单个字符
  3. [^] 匹配指定范围外的任意单个字符
  4. [:alnum:] 字母和数字
  5. [:alpha:] 代表任何英文大小写字符,亦即 A-Z, a-z
  6. [:lower:] 小写字母 [:upper:] 大写字母
  7. [:blank:] 空白字符(空格和制表符)
  8. [:space:] 水平和垂直的空白字符(比[:blank:]包含的范围广)
  9. [:cntrl:] 不可打印的控制字符(退格、删除、警铃...)
  10. [:digit:] 十进制数字 [:xdigit:]十六进制数字
  11. [:graph:] 可打印的非空白字符
  12. [:print:] 可打印字符
  13. [:punct:] 标点符号
  14. [root@centos7 ~]#grep -o "[[:digit:]]\+" /etc/centos-release|head -n1 //查看contos版本
  15. [root@centos7 ~]#grep -o "[0-9]\+" /etc/centos-release|head -n1 //查看contos版本

匹配次数

符要出现的次数 用在要指定次数的字符后面,用于指定前面的字

  1. * 匹配前面的字符任意次,包括0次贪婪模式:尽可能长的匹配(只表示*前面一个字符)
  2. .* 任意长度的任意字符(a.*;a后面所有)
  3. \? 匹配其前面的字符01次(可有可无;a\? aa都匹配!贪婪)
  4. \+ 匹配其前面的字符至少1
  5. \{n\} 匹配前面的字符n
  6. \{m,n\} 匹配前面的字符至少m次,至多n
  7. \{,n\} 匹配前面的字符至多n次(只要有就行)
  8. \{n,\} 匹配前面的字符至少n次(最少n次)

位置锚定:定位出现的位置

  1. ^ 行首锚定,用于模式的最左侧
  2. $ 行尾锚定,用于模式的最右侧
  3. ^PATTERN$ 用于模式匹配整行
  4. ^$ 空行 (grep -v "^$" f1 //匹配非空行)
  5. ^[[:space:]]*$ 空白行
  6. \< \b 词首锚定,用于单词模式的左侧(grep "\<r" /etc/passwd)(\b不是很精准比如"r\br")
  7. \> \b 词尾锚定;用于单词模式的右侧(grep "r\>" /etc/passwd)(\b不是很精准比如"r\br")
  8. \<PATTERN\> 匹配整个单词

分组:

  1. 分组: \(\) 将一个或多个字符捆绑在一起,当作一个整体进行处理,如: \(root\)\+
  2. 分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中,这些变量的命名方式为: \1, \2, \3, ...
  3. \1 表示从左侧起第一个左括号以及与之匹配右括号之间的模式所匹配到的字符
  4. 示例: \(string1\+\(string2\)*\)
  5. \1代表: string1\+\(string2\)*
  6. \2代表: string2
  7. 后向引用:引用前面的分组括号中的模式所匹配字符, 而非模式本身
  8. 或者: \|
  9. 示例: a\|b: ab C\|cat: Ccat \(C\|c\)at:Catcat
  1. [root@centos7 ~]#grep -o "^[[:alnum:]_]\+[[:space:]]*()" /etc/init.d/functions //去函数名称
  2. [root@centos7 ~]#grep ^[Ss].* /proc/meminfo
  3. SwapCached: 572 kB
  4. SwapTotal: 2097148 kB
  5. SwapFree: 2092372 kB
  6. Shmem: 5328 kB
  7. Slab: 106440 kB
  8. SReclaimable: 47528 kB
  9. SUnreclaim: 58912 kB
  10. [root@centos7 ~]#grep -i "^s" /proc/meminfo
  11. SwapCached: 584 kB
  12. SwapTotal: 2097148 kB
  13. SwapFree: 2092392 kB
  14. Shmem: 5348 kB
  15. Slab: 106448 kB
  16. SReclaimable: 47528 kB
  17. SUnreclaim: 58920 kB
  18. [root@centos7 ~]#grep -v "/bin/bash$" /etc/passwd
  19. [root@centos7 ~]#grep "^rpc\b" /etc/passwd |cut -d: -f7
  20. /sbin/nologin

练习题:

  1. 1.显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
  2. [root@centos7 ~]#grep -i "^s" /proc/meminfo
  3. [root@centos7 ~]#grep "^[Ss]" /proc/meminfo
  4. 2.显示/etc/passwd文件中不以/bin/bash结尾的行
  5. [root@centos7 ~]#grep -v "/bin/bash" /etc/passwd
  6. 3.显示用户rpc默认的shell程序
  7. [root@centos7 ~]#grep "^fei\>" /etc/passwd|cut -d: -f7
  8. 4.找出/etc/passwd中的两位或三位数
  9. [root@centos7 ~]#grep -o "\b[0-9]\{2,3\}\b" /etc/passwd
  10. 5.显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行
  11. [root@centos7 ~]#grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg
  12. 6.找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行
  13. [root@centos7 ~]#netstat -tna |grep "LISTEN[[:space:]]*$"
  14. 7.显示CentOS7上所有系统用户的用户名和UID
  15. [root@centos7 ~]#cut -d: -f1,3 /etc/passwd |grep "\b[0-9]\{1,3\}$"
  16. 8.添加用户bashtestbashbashershnologin(其shell为/sbin/nologin),找出/etc/passwd用户名同shell名的行
  17. grep "^\(.*\):.*\b\1$" /etc/passwd
  18. grep "^\(.*\):.*\<\1$" /etc/passwd
  19. grep "^\(.*\)\>.*\b\1$" /etc/passwd
  20. 9. 利用dfgrep,取出磁盘各分区利用率,并从大到小排序
  21. df |grep "/dev/sd" |grep -o "\<[[:digit:]]\{1,3\}%" |grep -o "[[:digit:]]\{1,3\}" |sort -nr
  22. 取出IP[root@centos7 ~]ifconfig ens33 |grep -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"

作业:

  1. 1、显示三个用户rootmagewangUID和默认shell
  2. [root@centos7 ~]#grep -e ^root -e ^fei -e ^bin /etc/passwd |cut -d: -f3,7
  3. 2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
  4. grep -o "^[[:alnum:]_]\+[[:space:]]*()" /etc/rc.d/init.d/functions
  5. grep "^[[:alnum:]_]\+[[:space:]]*()" /etc/rc.d/init.d/functions
  6. 3、使用egrep取出/etc/rc.d/init.d/functions中其基名
  7. echo "/etc/rc.d/init.d/" |egrep -o "[^/]+/?$"
  8. 4、使用egrep取出上面路径的目录名
  9. echo "/etc/rc.d/init.d/functions" |egrep -o "^/.*/"
  10. 5、统计last命令中以root登录的每个主机IP地址登录次数
  11. [root@centos7 ~]#last|grep root|egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"|sort |uniq -c
  12. 6、利用扩展正则表达式分别表示0-910-99100-199200-249250-255
  13. [0-9] [0-9][0-9] 1[0-9][0-9] 2[0-4][0-9] 25[0-5]
  14. 7、显示ifconfig命令结果中所有IPv4地址
  15. [root@centos7 ~]#ifconfig |egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"
  16. 8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到面
  17. echo "welcome to magedu linux"|grep -o "[[:alpha:]]"|sort|uniq -c|sort -nr
  18. 9、用正则表达式表示出QQ
  19. Egrep [0-9]{5,10}
  20. 10、用正则表达式表示出身份证号
  21. Egrep [0-9]{17}[0-9X]
  22. 11、用正则表达式表示手机号
  23. Egrep 1[0-9]]{10}
  24. 12、用正则表达式表示邮箱: x@y.z.m
  25. grep "\b\([[:alnum:]\_\.]\)\{1,\}@\(\([[:alnum:]_]\)\+\.\)\{1,\}\([[:alnum:]_]\)\{1,\}\b"
  26. 1、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符
  27. 2、复制/etc/rc.d/init.d/functions文件至/tmp目录,用查找替换命令为/tmp/functions的每行开头为空白字符的行的行首添加一个#号
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注