一. 回顾 回顾1 shell常用命令 
1 2 3 获取命令的返回输出结果	 1.` `,反引号	 line=`ls` 2.$()			line=$(ls) 
1 2 3 4 seq 类似于python里的range 主要生产一组有序数字序列 -s 指定分隔符 -w 指定同等宽度输出 
二. sort sort命令 
默认按每行的第一个字符排序
-n:按整数 进行排序–>默认是升序 
-r:递减 排序(注:降序)
-u:去重
指定排序键
指定按哪一列数据进行排序
-k:指定哪一列为排序键
cat tt | sort -n -k4
指定字段分隔符
sort是一个排序命令 
默认按照每行第一个首字符进行排序 
英文根据a-z的顺序进行排序,如果第一个字母相同,就比较第二个字母,依次类推 中文根据首个字符的拼音的首字母进行排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 示例1:cat  sort_test.txt |sort  --------------------------------------------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# cat  sort_test.txt 			 中文 456 1xx  123 abc bcd  3yy dd  Aac XYZ  2zz 三创 xixi [root@sanchuang-linux ~]# cat  sort_test.txt |sort 		 三创 xixi 中文 456 1xx  123 Aac XYZ  2zz abc bcd  3yy dd  -------------------------------------------------------------------------------------------- >>> ord("三" )							 19977									 >>> ord("中" ) 20013 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# locale		 LANG=zh_CN.UTF-8 LC_CTYPE="zh_CN.UTF-8"  LC_NUMERIC="zh_CN.UTF-8"  LC_TIME="zh_CN.UTF-8"  LC_COLLATE="zh_CN.UTF-8"  LC_MONETARY="zh_CN.UTF-8"  LC_MESSAGES="zh_CN.UTF-8"  LC_PAPER="zh_CN.UTF-8"  LC_NAME="zh_CN.UTF-8"  LC_ADDRESS="zh_CN.UTF-8"  LC_TELEPHONE="zh_CN.UTF-8"  LC_MEASUREMENT="zh_CN.UTF-8"  LC_IDENTIFICATION="zh_CN.UTF-8"  LC_ALL= ============================================================================================ 示例2:cat  sort_test.txt |sort  -k 2	指定哪一列为排序键 [root@sanchuang-linux ~]# cat  sort_test.txt |sort  -k2		 中文 456 1xx  123 abc bcd  3yy dd  三创 xixi Aac XYZ  2zz [root@sanchuang-linux ~]# cat  sort_test.txt |sort  -k 3	   三创 xixi													 中文 456 1xx  123											 Aac XYZ  2zz											  abc bcd  3yy dd 											  ============================================================================================ 示例3:英文根据a-z的顺序进行排序,如果第一个字母相同,就比较第二个字母 [root@sanchuang-linux ~]# cat  sort_test.txt  中文 456 1xx  123 aac bcd  3yy dd  Aac XYZ  2zz 三创 xixi Xyz  cde Bbc  Abc bbc xxx ABC abc [root@sanchuang-linux ~]# cat  sort_test.txt |sort 			 三创 xixi 中文 456 1xx  123 aac bcd  3yy dd  Aac XYZ  2zz abc ABC Bbc  Abc bbc xxx Xyz  cde 
sort -n sort -n 按数字进行排序 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [root@sanchuang-linux ~]# a=123						 [root@sanchuang-linux ~]# b=234 [root@sanchuang-linux ~]# echo  $a +$b 				 123+234 [root@sanchuang-linux ~]# echo  $(($a +$b ))			 357 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# cat  aa.txt  123 23 4 234 [root@sanchuang-linux ~]# cat  aa.txt |sort 			 123 23 234 4 [root@sanchuang-linux ~]# cat  aa.txt |sort  -n		 4													 23													 123 234 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# cat  aa.txt |sort  -n -r	 [root@sanchuang-linux ~]# cat  aa.txt |sort  -nr		 234 123 23 4 
-t 指定列数的分隔符 指定列数的分隔符 # head -n7 /etc/passwd |sort -k6 -t : 
 默认分隔符为空白字符 
 使用 -t 指定列数的分隔符 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [root@sanchuang-linux ~]# head  -n7 /etc/passwd |sort 		 adm:x:3:4:adm:/var/adm:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sync :x:5:0:sync :/sbin:/bin/sync[root@sanchuang-linux ~]# head  -n7 /etc/passwd |sort  -k2	 adm:x:3:4:adm:/var/adm:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sync :x:5:0:sync :/sbin:/bin/sync[root@sanchuang-linux ~]# head  -n7 /etc/passwd |sort  -k6 -t :			 bin:x:1:1:bin:/bin:/sbin/nologin root:x:0:0:root:/root:/bin/bash sync :x:5:0:sync :/sbin:/bin/syncdaemon:x:2:2:daemon:/sbin:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@sanchuang-linux ~]# head  -n7 /etc/passwd |sort  -k6 -t : -r		 
三. 练习:找出内存使用率最高的5个进程 找出内存使用率最高的5个进程 
1 2 3 4 5 6 7 8 9 10 11 12 13 ps aux|sort  -n -k4 -r|head  -5		注:推荐 注:内存使用率 %MEM [root@sanchuang-linux ~]# ps aux|tail  -n +2|sort  -nr -k4 |head  -5 root         960  0.0  2.0 221572 38096 ?        S    08:31   0:00 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files root         930  0.0  1.7 425416 31480 ?        Ssl  08:31   0:01 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P polkitd      890  0.0  1.2 1625936 23856 ?       Ssl  08:31   0:00 /usr/lib/polkit-1/polkitd --no-debug root         891  0.0  0.9 391216 18088 ?        Ssl  08:31   0:00 /usr/sbin/NetworkManager --no-daemon root         954  0.0  0.8 219700 15416 ?        S    08:31   0:00 /usr/libexec/sssd/sssd_be --domain implicit_files --uid 0 --gid 0 --logger=files -------------------------------------------------------------------------------------------- 注:tail  -n +2 显示从第2行到末尾(可加可不加) 注:sort  -nr -k4   -n按数字排序,-r倒序,-k4指定第4列为排序键 注:head  -5 取前5行 
四. uniq uniq命令的使用(去重) 
uniq --> unique唯一的 去重相邻的行先排序,再去重 -c 统计重复出现的次数 -u 显示只出现1次的行 -d 显示重复出现的行 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 ============================================================================================ 示例1:去重相邻的行 [root@sanchuang-linux ~]# cat  uniq_test.txt 123 abc abc 123 45 46 45 45 47 47 48 47 [root@sanchuang-linux ~]# cat  uniq_test.txt |uniq 		 123 abc abc 123 45 46 45 47 48 47 [root@sanchuang-linux ~]# cat  uniq_test.txt |sort  -n |uniq 	 abc 123														 45 46 47 48 123 abc -------------------------------------------------------------------------------------------- 示例1.1  [root@sanchuang-linux ~]# cat  uniq_test.txt |sort  -n -u		 [root@sanchuang-linux ~]# cat  uniq_test.txt |sort  -nu		 abc 123 45 46 47 48 123 abc ============================================================================================ 示例2:-c 统计重复出现 [root@sanchuang-linux ~]# cat  uniq_test.txt |sort  -n |uniq  -c           1 abc 123       3 45       1 46       3 47       1 48       1 123 abc ============================================================================================ 示例3:-u显示只出现1次的行 [root@sanchuang-linux ~]# cat  uniq_test.txt |sort  -n |uniq  -u	 abc 123 46 48 123 abc ============================================================================================ 示例4:-d 显示重复出现的行 [root@sanchuang-linux ~]# cat  uniq_test.txt |sort  -n |uniq  -d	 45 47 
五. 练习:统计120000行 频率前十的ip 统计120000行 频率前十的ip 
1 2 3 4 5 [root@sanchuang-linux ~]# cat  ips.txt |sort  |uniq  -c|sort  -nr |head  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 注 :sort  -c 用于统计ip地址的访问 [root@localhost ~]# yum install nginx [root@sanchuang-linux ~]# nginx [root@sanchuang-linux ~]# lsof -i:80			 COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME nginx   12765  root    9u  IPv4  60060      0t0  TCP *:http (LISTEN) nginx   12765  root   10u  IPv6  60061      0t0  TCP *:http (LISTEN) nginx   12766 nginx    9u  IPv4  60060      0t0  TCP *:http (LISTEN) nginx   12766 nginx   10u  IPv6  60061      0t0  TCP *:http (LISTEN) nginx   12767 nginx    9u  IPv4  60060      0t0  TCP *:http (LISTEN) nginx   12767 nginx   10u  IPv6  60061      0t0  TCP *:http (LISTEN) [root@sanchuang-linux ~]# iptables -F			 [root@sanchuang-linux ~]# cd  /var/log			 [root@sanchuang-linux log ]# cd  nginx [root@sanchuang-linux nginx]# pwd  /var/log/nginx [root@sanchuang-linux nginx]# ls  access.log  error.log							 
六. 练习:统计web服务器访问前十的用户 统计web服务器访问前十的用户(注:通过ip判断) 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@sanchuang-linux nginx]# head  access.log  192.168.0.42 - - [29/Oct/2020:12:01:01 +0800] "GET / HTTP/1.1"  200 4057 "-"  "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3823.400 QQBrowser/10.7.4307.400"  "-"  ………………						 [root@sanchuang-linux nginx]# cat  access.log |awk '{print $1}'  192.168.0.42				 192.168.0.42 192.168.0.42 192.168.0.42 192.168.0.42 192.168.0.193 192.168.0.193 192.168.0.193 192.168.0.193				 [root@sanchuang-linux nginx]# cat  access.log |awk '{print $1}' |sort |uniq  -c|sort  -nr|head  -3       5 192.168.0.42		       4 192.168.0.193 
七. cut cut命令 
从文本文件或者文本流中提取文本列
cut -选项 提取范围 文本文件
-----------------------------------
常见选项 
-c:从指定提取范围中提取字符
-f:从指定提取范围中提取字段
-d:指定分隔符,默认分隔符为tab键
-----------------------------------
提取范围 
n:第n项
n-:第n项到行尾
-m:行首到第m项
n,m:第n项和第m项
n-m:第n项到第m项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 示例 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# w					  14:45:33 up  4:12,  5 users ,  load average: 0.00, 0.00, 0.03 USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT root     tty1     -                三22   15:49   0.08s  0.08s -bash root     pts/0    192.168.0.42     14:29    3.00s  0.02s  0.01s w root     pts/1    192.168.0.42     14:29   15:35   0.00s  0.00s -bash root     pts/3    192.168.0.42     09:48    4:56m  0.03s  0.03s -bash root     pts/4    192.168.0.42     09:50    2:37m  0.52s  0.52s -bash [root@sanchuang-linux ~]# who 				 root     tty1         2020-10-28 22:15 root     pts/0        2020-10-29 14:29 (192.168.0.42) root     pts/1        2020-10-29 14:29 (192.168.0.42) root     pts/3        2020-10-29 09:48 (192.168.0.42) root     pts/4        2020-10-29 09:50 (192.168.0.42) 方法1 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# w |tr  -s " " |cut  -d" "  -f 1,2,4  14:50:31 4:17,									 USER TTY LOGIN@									 root tty1 三22								    root pts/0 14:29 root pts/1 14:29 root pts/3 09:48 root pts/4 09:50 方法2 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# w |awk '{print $1,$2,$4}' 		 14:51:28 up 5										     USER TTY LOGIN@ root tty1 三22 root pts/0 14:29 root pts/1 14:29 root pts/3 09:48 root pts/4 09:50 
以冒号作为分隔符 截取用户名、用户Id、用户属组 以冒号作为分隔符 截取用户名、用户Id、用户属组 
1 2 3 4 5 写法1 [root@sanchuang-linux ~]# cat  /etc/passwd|cut  -d":"  -f 1,3,5	 写法2(推荐) [root@sanchuang-linux ~]# cut  -d ":"  -f 1,3,5 /etc/passwd	 
取值范围 取值范围 
1 2 3 4 5 6 7 8 9 10 11 12 13 [root@sanchuang-linux ~]# cut  -d ":"  -f 1,3,5 /etc/passwd	 [root@sanchuang-linux ~]# cut  -d ":"  -f 1-5 /etc/passwd		 [root@sanchuang-linux ~]# cut  -d ":"  -f 3- /etc/passwd		 [root@sanchuang-linux ~]# cut  -d ":"  -f -3 /etc/passwd		 ============================================================================================ -c:从指定提取范围中提取字符 示例 [root@sanchuang-linux ~]# echo  abcdefg |cut  -c 2		 b [root@sanchuang-linux ~]# echo  abcdefg |cut  -c 2-5		 bcde [root@sanchuang-linux ~]# echo  abcdefg |cut  -c 5-		 efg 
八. 练习 练习 
1 2 3 4 5 1统计access.log中排名前三的ip 2显示/boot目录下面所有的文件大小(包括子目录中的文件),并且由小到大排序 3统计/etc/passwd中每种shell使用的次数(降序排序) 4统计一下/etc/passwd中sbin这个单词出现了多少次 5只显示ens33的ip地址 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 ============================================================================================ 示例1:1统计access.log中排名前三的ip -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# cut  -d "-"  -f 1 access.log |sort |uniq  -c|sort  -nr|head  -n3       7 192.168.0.42        6 192.168.0.193        5 192.168.0.38  -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# cut  -d " "  -f1 access.log |sort |uniq  -c       6 192.168.0.193       5 192.168.0.21       5 192.168.0.32       5 192.168.0.37       5 192.168.0.38       7 192.168.0.42 [root@sanchuang-linux ~]# cut  -d " "  -f1 access.log |sort |uniq  -c|sort  -nr       7 192.168.0.42       6 192.168.0.193       5 192.168.0.38       5 192.168.0.37       5 192.168.0.32       5 192.168.0.21 [root@sanchuang-linux ~]# cut  -d " "  -f1 access.log |sort |uniq  -c|sort  -nr|head  -3       7 192.168.0.42       6 192.168.0.193       5 192.168.0.38 ============================================================================================ 示例2:2显示/boot目录下面所有的文件大小(包括子目录中的文件),并且由小到大排序 方法1 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# du  -ak /boot |sort  -n  		 4	/boot/.bashrc 4	/boot/efi/EFI/centos 4	/boot/grub2/device.map 4	/boot/grub2/grubenv 4	/boot/grub2/i386-pc/adler32.mod 4	/boot/grub2/i386-pc/all_video.mod 4	/boot/grub2/i386-pc/aout.mod 方法2 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# ll -R|grep root|tr  -s " " |cut  -d " "  -f 5,9|sort  -n 0 1214.txt 0 12244.txt 0 1224.txt 0 12456.txt 0 20 0 20 0 2020-09-24-18_25_03.txt 方法3 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# ll -R|grep root|awk '{print $5,$9}' |sort  -n 0 1214.txt 0 12244.txt 0 1224.txt 0 12456.txt 0 20 0 20 0 2020-09-24-18_25_03.txt 0 abcd.txt ============================================================================================ 示例3:3统计/etc/passwd中每种shell使用的次数(降序排序) [root@sanchuang-linux ~]# cut  -d : -f7 /etc/passwd |sort |uniq  -c|sort  -nr      31 /bin/bash      19 /sbin/nologin       1 /sbin/shutdown       1 /sbin/halt       1 /bin/sync ============================================================================================ 示例4:4统计一下/etc/passwd中sbin这个单词出现了多少次 方法1 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# grep -o sbin /etc/passwd sbin sbin ……………… sbin sbin [root@sanchuang-linux ~]# grep -o sbin /etc/passwd|wc  -l 25 方法2 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# cat  /etc/passwd|tr  ":"  "\n" |grep sbin|wc  -l 25						 ============================================================================================ 示例5:5只显示ens33的ip地址 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# ip a |grep ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000     inet 192.168.0.34/24 brd 192.168.0.255 scope global dynamic noprefixroute ens33 [root@sanchuang-linux ~]# ip a |grep ens33|grep inet|tr  -s " " |cut  -d " "  -f3 192.168.0.34/24 [root@sanchuang-linux ~]# ip a |grep ens33|grep inet|tr  -s " " |cut  -d " "  -f3|cut  -d"/"  -f1 192.168.0.34 
九. awk指定分隔符 -F awk指定分隔符 -F 
1 2 3 4 5 6 7 8 [root@sanchuang-linux ~]# awk -F":"  '{print $1}'  /etc/passwd root bin daemon adm lp sync ………………………… 
十. grep grep命令 
文本三剑客 ==> awk grep sedhttps://www.cnblogs.com/end/archive/2012/02/21/2360965.html  grep 过滤 通用的正则表达式分析程序grep、egrep、fgrep 做匹配来过滤的 
用途:在文件中查找并显示包含指定字符串的行 格式:grep [选项]... 模式 目标文件
#注:模式pattern --》模板 
 可以接受一个正则表达式 
-i:查找时忽略大小写
-v:反转查找,输出与模式不相符的行
-n:显示符合模式要求的行号
-r:递归搜索所有文件
-o:只显示匹配的内容
-E:支持更多的元字符(支持扩展正则)
-A:找到匹配行以及后几行
-B:输出匹配行以及前几行
模式
^…. :以什么开头,整行以什么开头
…..$ :以什么结尾,整行以什么结尾
注:grep 是文本操作命令,可以直接操作文本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 ===================================================================== 示例1:-v:反转查找,输出与模式不相符的行 --------------------------------------------------------------------- [root@sanchuang-linux ~]# grep -v “#” /etc/yum.repos.d/centos.repo    ============================================================================================ 示例2:-i 查找时忽略大小写 --------------------------------------------------------------------- [root@sanchuang-linux ~]# cd  /etc/ssh/ [root@sanchuang-linux ssh]# pwd  /etc/ssh [root@sanchuang-linux ssh]# grep -i "port"  /etc/ssh/sshd_config 		 ============================================================================================ 示例3:忽略大小写,并显示查找到的行号 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ssh]# grep -i -n "port"  /etc/ssh/sshd_config 		 13:# If you want to change the port on a SELinux system, you have to tell 15:# semanage port -a -t ssh_port_t -p tcp  17:#Port 22 102:# WARNING: 'UsePAM no'  is not supported in  Fedora and may cause several 108:#GatewayPorts no ============================================================================================ 示例4:-r 递归搜索所有文件 注:子目录以及子子目录下面查找 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ssh]# grep "xxxxx"  * -r  				 [root@sanchuang-linux ssh]# grep "GET"  /var/log/nginx -r  	 /var/log/nginx/error.log:2020/10/29 12:01:02 [error] 12767#0: *2 open() "/usr/share/nginx/html/favicon.ico"  failed (2: No such file or directory), client: 192.168.0.42, server: _, request: "GET /favicon.ico HTTP/1.1" , host: "192.168.0.34" , referrer: "http://192.168.0.34/"  /var/log/nginx/error.log:2020/10/29 12:01:58 [error] 12767#0: *2 open() "/usr/share/nginx/html/favicon.ico"  failed (2: No such file or directory), client: 192.168.0.42, server: _, request: "GET /favicon.ico HTTP/1.1" , host: "192.168.0.34"  -------------------------------------------------------------------------------------------- [root@mysql-binary nginx]# grep "GET"  /var/log/nginx -r 
十一. 正则表达式 正则表达式 
^aa 表示以aa开头的行aa$ 表示以aa结尾的行
1 2 3 4 []      表示一个字符集 [a-z]   从a - z 中取一个 [^a-z]  不取a-z的字符 grep ^[^a-zA-Z0-9_] grep_test.txt  显示不以字母、数字、下划线开头的行 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 示例1:不输出以#开头的行 -------------------------------------------------------------------------------------------- [root@sanchuang-linux yum.repos.d]# grep -v ^# centos.repo [root@sanchuang-linux yum.repos.d]# grep -v ^# centos.repo|grep -v ^$    注:不输出空白行       grep -v ^$    不输出以#开头的行  grep -v ^# ============================================================================================ 示例2:过滤出grep_test.txt中,不以#号开头的行 和 非空白行 -------------------------------------------------------------------------------------------- [root@sanchuang-linux chenpeng]# cat  grep_test.txt aaa#bbb 456 789 [root@sanchuang-linux chenpeng]# grep -v ^# grep_test.txt   aaa#bbb 456 789 [root@sanchuang-linux chenpeng]# grep -v ^# grep_test.txt |grep -v ^$	 aaa#bbb                                       456 789 过滤出grep_test.txt中 不以#开头的行和非空白行 [root@sanchuang-linux chenpeng]# grep -v -E "^#|^$"  grep_test.txt  aaa#bbb                          456                              789 
十二. [] 表示一个字符集(正则表达式) [] 表示一个字符集(正则表达式) 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 [root@localhost chenpeng]# cat  grep_test.txt  abc adc Abdc ac a1c axy axc 123 777 示例1:过滤出abc adc -------------------------------------------------------------------------------------------- [root@localhost chenpeng]# grep a[bd]c grep_test.txt         abc adc [root@sanchuang-linux chenpeng]# grep a[a-z]c grep_test.txt  abc                                         adc                                         axc [root@sanchuang-linux chenpeng]# grep a[0-9]c grep_test.txt      a1c -------------------------------------------------------------------------------------------- 示例2:[^a-z] 不取a-z的字符 -------------------------------------------------------------------------------------------- [root@sanchuang-linux chenpeng]# grep a[^a-z]c grep_test.txt     a1c 示例3:取出不以字母开头的行 -------------------------------------------------------------------------------------------- [root@sanchuang-linux chenpeng]# grep ^[^a-zA-Z] grep_test.txt   11c 123                                    777 注:grep ^[a-zA-Z] grep_test.txt  以字母开头的行 ============================================================================================ 示例4:显示不以字母、数字、下划线开头的行 写法1 [root@sanchuang-linux chenpeng]# grep ^[^a-zA-Z0-9_] grep_test.txt 写法2 [root@sanchuang-linux chenpeng]# grep -v ^[a-zA-Z0-9_] grep_test.txt 
十三. 通配符(正则表达式) 通配符(正则表达式) 
1 2 3 4 5 6 7 *     代表匹配前一个项任意次 ?    代表匹配前一个项0次或者1次 +     代表匹配前一个项一次到多次 .     占位符 除\n之外的任意字符 {n,m} 匹配前一项n到m次 egrep 等同于 grep -E fgrep 不支持任何正则,普通文本过滤 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 示例1:* ? + . -------------------------------------------------------------------------------------------- [root@localhost ~]# cat  grep_test.txt  alc axxc ac [root@localhost ~]# grep -E a.?c grep_test.txt     alc                                                ac                                                 [root@localhost ~]# grep -E a.*c grep_test.txt     alc                axxc ac [root@localhost ~]# grep -E a.c grep_test.txt      alc [root@localhost ~]# grep -E a.+c grep_test.txt     alc axxc 示例2:{ } -------------------------------------------------------------------------------------------- [root@localhost ~]# egrep "a.{1}c"  grep_test.txt     alc [root@localhost ~]# egrep "a.{1,2}c"  grep_test.txt   alc axxc 示例3:{ } -------------------------------------------------------------------------------------------- [root@localhost ~]# cat  grep_test.txt  alc axxc ac ayy1c addddddc [root@localhost ~]# egrep "a.{1,5}c"  grep_test.txt            alc axxc ayy1c egrep 等同于 grep -E fgrep  不支持正则,普通文本过滤 
十四. 练习:grep 正则表达式 grep 正则表达式 
1、进入/lianxi目录,复制/etc/passwd到当前目录下,然后对passwd进行操作
2、查找出当前passwd文件中以ftp或者mail开头的行,输出到屏幕
1 2 grep -E "^ftp|^mail"  passwd egrep "^ftp|^mail"  passwd 
3、查找出当前passwd文件中不以r、m、f开头的行
1 2 grep -v -E "^r|^m|^f"  passwd grep ^[^rmp] passwd 
4、查找出当前passwd中以bash结尾的行
5、查找出/etc/login.defs文件中的有效行(不显示空行和注释行,以#号开头的行)
1 grep -v -E "^#|^$"  /etc/login.defs 
6、查找出/var/log/messages 文档中有15个字母的单词
1 2 grep -E "[^a-zA-Z][a-zA-Z{15}][^a-zA-Z]"  /var/log/message   grep -E "\b[a-zA-Z{15}]\b"  /var/log/message                 
7、查找出/etc/passwd文件里用户名包含liu同时使用bash的用户
1 grep liu /etc/passwd|grep bash$|cut  -d":"  -f1 
8、查找出/etc/ssh/sshd_config里的有效行
1 grep -v -E "^#|^$"  /etc/ssh/sshd_config 
9、查找出/etc/ssh/sshd_config 文件里包含连续2个字符的行
1 2 grep -E "(.)\1"  /etc/ssh/sshd_config  grep -E "(.)\1"    
10、查找出包含特殊字符的行
1 grep -E "[^0-Z]"  grep_test.txt 
11、查找出不包含数字的行
12、查找出/var/log/secure里的ip地址
1 cut  -d " "  -f11 /var/log/secure|grep -E "\.." |sort |uniq     注:\.转义  第二个. 表示任1字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -------------------------------------------------------------------------------------------- grep -E "((([0-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))"  ip_test.txt ============================================================================================ ip地址匹配:    Ipv4  0-255  4个    192.168.1.0 注:匹配ip地址 [root@sanchuang-linux ~]# grep -E "((([0-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9])|([1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))"  ip_test.txt 192.168.0.1 192.168.1.255 172.0.0.1 ((([0 -9 ])|([1 -9 ][0 -9 ])|(1 [0 -9 ][0 -9 ])|(2 [0 -4 ][0 -9 ])|(25 [0 -5 ]))\.){3}(([0 -9 ])|([1 -9 ][0 -9 ])|(1 [0 -9 ][0 -9 ])|(2 [0 -4 ][0 -9 ])|(25 [0 -5 ])) 分析 0-255 0-9                                     个位数 [1-9][0-9]                              十位数 (1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])   百位数