@mrz1
2018-01-01T14:14:14.000000Z
字数 5072
阅读 912
笔记
- 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 [OPTIONS] PATTERN [FILE...]
--color=auto: 对匹配到的文本着色显示 contos6手动添加
-v: 显示不被pattern匹配到的行(取反)
-i: 忽略字符大小写
-n: 显示匹配的行号
-c: 统计匹配的行数
-o: 仅显示匹配到的字符串
-q: 静默模式,不输出任何信息(后期在详解)
-A #: after, 后#行
-B #: before, 前#行
-C #: context, 前后各#行
-e:实现多个选项间的逻辑or关系
grep –e ‘cat ‘ -e ‘dog’ file
-w:匹配整个单词(数字,字母,下划线不能作为分隔符)
-E:使用ERE
-F:相当于fgrep,不支持正则表达式
[root@centos6 ~]#alias grep //alias grep='grep --color=auto'
[root@centos7 ~]#rpm -i /misc/cd/Packages/nmap-6.40-7.el7.x86_64.rpm //安装扫描系统漏洞
[root@centos7 ~]#nmap -v -sP 172.18.0.0/24 //扫描网段
[root@centos7 ~]#nmap -v -sP 172.18.0.0/24|grep -B1 up|grep "Nmap scan"|cut -d " " -f5
grep root /etc/passwd
grep "$USER" /etc/passwd
grep '$USER' /etc/passwd //不行单引号是字符串
grep `whoami` /etc/passwd
REGEXP:由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符字面意义,而表示控制或通配的功能
程序支持: grep,sed,awk,vim, less,nginx,varnish等
分两类:
基本正则表达式: BRE
扩展正则表达式: ERE
grep -E;egrep
正则表达式引擎:
采用不同算法,检查处理正则表达式的软件模块
PCRE(Perl(语言很牛掰和diff发明同一人) Compatible Regular Expressions)
元字符分类:字符匹配、匹配次数、位置锚定、分组
man 7 regex 查看详细信息
. 匹配任意单个字符
[] 匹配指定范围内的任意单个字符
[^] 匹配指定范围外的任意单个字符
[:alnum:] 字母和数字
[:alpha:] 代表任何英文大小写字符,亦即 A-Z, a-z
[:lower:] 小写字母 [:upper:] 大写字母
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的范围广)
[:cntrl:] 不可打印的控制字符(退格、删除、警铃...)
[:digit:] 十进制数字 [:xdigit:]十六进制数字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 标点符号
[root@centos7 ~]#grep -o "[[:digit:]]\+" /etc/centos-release|head -n1 //查看contos版本
[root@centos7 ~]#grep -o "[0-9]\+" /etc/centos-release|head -n1 //查看contos版本
符要出现的次数 用在要指定次数的字符后面,用于指定前面的字
* 匹配前面的字符任意次,包括0次贪婪模式:尽可能长的匹配(只表示*前面一个字符)
.* 任意长度的任意字符(a.*;a后面所有)
\? 匹配其前面的字符0或1次(可有可无;a\? 有a无a都匹配!贪婪)
\+ 匹配其前面的字符至少1次
\{n\} 匹配前面的字符n次
\{m,n\} 匹配前面的字符至少m次,至多n次
\{,n\} 匹配前面的字符至多n次(只要有就行)
\{n,\} 匹配前面的字符至少n次(最少n次)
^ 行首锚定,用于模式的最左侧
$ 行尾锚定,用于模式的最右侧
^PATTERN$ 用于模式匹配整行
^$ 空行 (grep -v "^$" f1 //匹配非空行)
^[[:space:]]*$ 空白行
\< 或 \b 词首锚定,用于单词模式的左侧(grep "\<r" /etc/passwd)(\b不是很精准比如"r\br")
\> 或 \b 词尾锚定;用于单词模式的右侧(grep "r\>" /etc/passwd)(\b不是很精准比如"r\br")
\<PATTERN\> 匹配整个单词
分组: \(\) 将一个或多个字符捆绑在一起,当作一个整体进行处理,如: \(root\)\+
分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中,这些变量的命名方式为: \1, \2, \3, ...
\1 表示从左侧起第一个左括号以及与之匹配右括号之间的模式所匹配到的字符
示例: \(string1\+\(string2\)*\)
\1代表: string1\+\(string2\)*
\2代表: string2
后向引用:引用前面的分组括号中的模式所匹配字符, 而非模式本身
或者: \|
示例: a\|b: a或b C\|cat: C或cat \(C\|c\)at:Cat或cat
[root@centos7 ~]#grep -o "^[[:alnum:]_]\+[[:space:]]*()" /etc/init.d/functions //去函数名称
[root@centos7 ~]#grep ^[Ss].* /proc/meminfo
SwapCached: 572 kB
SwapTotal: 2097148 kB
SwapFree: 2092372 kB
Shmem: 5328 kB
Slab: 106440 kB
SReclaimable: 47528 kB
SUnreclaim: 58912 kB
[root@centos7 ~]#grep -i "^s" /proc/meminfo
SwapCached: 584 kB
SwapTotal: 2097148 kB
SwapFree: 2092392 kB
Shmem: 5348 kB
Slab: 106448 kB
SReclaimable: 47528 kB
SUnreclaim: 58920 kB
[root@centos7 ~]#grep -v "/bin/bash$" /etc/passwd
[root@centos7 ~]#grep "^rpc\b" /etc/passwd |cut -d: -f7
/sbin/nologin
1.显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
[root@centos7 ~]#grep -i "^s" /proc/meminfo
[root@centos7 ~]#grep "^[Ss]" /proc/meminfo
2.显示/etc/passwd文件中不以/bin/bash结尾的行
[root@centos7 ~]#grep -v "/bin/bash" /etc/passwd
3.显示用户rpc默认的shell程序
[root@centos7 ~]#grep "^fei\>" /etc/passwd|cut -d: -f7
4.找出/etc/passwd中的两位或三位数
[root@centos7 ~]#grep -o "\b[0-9]\{2,3\}\b" /etc/passwd
5.显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行
[root@centos7 ~]#grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg
6.找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行
[root@centos7 ~]#netstat -tna |grep "LISTEN[[:space:]]*$"
7.显示CentOS7上所有系统用户的用户名和UID
[root@centos7 ~]#cut -d: -f1,3 /etc/passwd |grep "\b[0-9]\{1,3\}$"
8.添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名同shell名的行
grep "^\(.*\):.*\b\1$" /etc/passwd
grep "^\(.*\):.*\<\1$" /etc/passwd
grep "^\(.*\)\>.*\b\1$" /etc/passwd
9. 利用df和grep,取出磁盘各分区利用率,并从大到小排序
df |grep "/dev/sd" |grep -o "\<[[:digit:]]\{1,3\}%" |grep -o "[[:digit:]]\{1,3\}" |sort -nr
取出IP[root@centos7 ~]ifconfig ens33 |grep -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"
1、显示三个用户root、mage、wang的UID和默认shell
[root@centos7 ~]#grep -e ^root -e ^fei -e ^bin /etc/passwd |cut -d: -f3,7
2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
grep -o "^[[:alnum:]_]\+[[:space:]]*()" /etc/rc.d/init.d/functions
grep "^[[:alnum:]_]\+[[:space:]]*()" /etc/rc.d/init.d/functions
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
echo "/etc/rc.d/init.d/" |egrep -o "[^/]+/?$"
4、使用egrep取出上面路径的目录名
echo "/etc/rc.d/init.d/functions" |egrep -o "^/.*/"
5、统计last命令中以root登录的每个主机IP地址登录次数
[root@centos7 ~]#last|grep root|egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"|sort |uniq -c
6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
[0-9] [0-9][0-9] 1[0-9][0-9] 2[0-4][0-9] 25[0-5]
7、显示ifconfig命令结果中所有IPv4地址
[root@centos7 ~]#ifconfig |egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"
8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到面
echo "welcome to magedu linux"|grep -o "[[:alpha:]]"|sort|uniq -c|sort -nr
9、用正则表达式表示出QQ号
Egrep [0-9]{5,10}
10、用正则表达式表示出身份证号
Egrep [0-9]{17}[0-9X]
11、用正则表达式表示手机号
Egrep 1[0-9]]{10}
12、用正则表达式表示邮箱: x@y.z.m
grep "\b\([[:alnum:]\_\.]\)\{1,\}@\(\([[:alnum:]_]\)\+\.\)\{1,\}\([[:alnum:]_]\)\{1,\}\b"
1、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符
2、复制/etc/rc.d/init.d/functions文件至/tmp目录,用查找替换命令为/tmp/functions的每行开头为空白字符的行的行首添加一个#号