正则表达式以及字符处理命令
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。在我们运维工作中,难免会用到正则表达式,尤其是做网站运维的童鞋,在处理一些网页的时候,我们在shell脚本里面用正则表达式是再常见不过啦~
许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。
一.正则表达式与通配符
1>.正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配。grep,awk,sed等命令可以支持正则表达式;
2>.通配符用来匹配符合条件的文件名,通配符是完全匹配。ls,find,cp这些命令不支持正则表达式,所以只能使用shell自己的通配符来进行匹配了;
二.基础正则表达式

更多关于正则表达式,请参考百度大大:https://baike.baidu.com/item/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/1700215?fr=aladdin
三.字符截取命令
1.cut字段提取命令
该命令主要用来截取特定的字符串,主要实现的是对列的截取,将一个文本的每一行拿来单独进行截取操作。

用法展示1:

用法展示2:
1 [root@yinzhengjie ~]# cut -d ":" -f 1,3 /etc/passwd | tail -5 2 [root@yinzhengjie ~]# cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1
2.printf命令
该命令可以让用户自定义格式化输入,shell编程中的printf跟golang语言的“fmt.printf”方法很像。当然还有一个print命令,这个命令不需要用户自定义格式化输出,因为它会在用户输入的字符串添加一个换行符,print的功能和golang的“fmt.println”很像。

案例展示1:

案例展示2:

3.awk命令
awk主要是对字符串进行列截取,不过它的功能甩cut命令几条街哟~只要是cut能实现的它都能轻松的搞定,不过我们应该浅尝辄止,因为awk其实是一个单独的编程语言,不过我们只需要用shell来实现流程控制,用awk截取我们想要的字段即可,所以说我们这里介绍的只是awk的冰山一角。
a>.awk的基本使用;

案例展示1:(awk中print会自动添加换行符)
1 [root@yinzhengjie ~]# df -h 2 Filesystem Size Used Avail Use% Mounted on 3 /dev/sda2 18G 4.8G 12G 29% / 4 tmpfs 491M 228K 491M 1% /dev/shm 5 /dev/sda1 283M 28M 240M 11% /boot 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# df -h | awk '{print $1 "\t" $5 "\t" $6}' 9 Filesystem Use% Mounted 10 /dev/sda2 29% / 11 tmpfs 1% /dev/shm 12 /dev/sda1 11% /boot 13 [root@yinzhengjie ~]#
案例展示二:(awk中printf是不会添加换行符的,需要你手动输入。)
1 [root@yinzhengjie ~]# cat student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 1 yzj boy 98 96 97 95 96.5 4 2 jay boy 95 98 95 96 96 5 3 ainy girl 90 99 93 98 95 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# 9 [root@yinzhengjie ~]# awk '{printf $2"\t"$6"\n"}' student.txt 10 Name Linux 11 yzj 97 12 jay 95 13 ainy 93 14 [root@yinzhengjie ~]#
案例展示三:(判断根分区的使用情况)
1 [root@yinzhengjie ~]# df -h 2 Filesystem Size Used Avail Use% Mounted on 3 /dev/sda2 18G 4.8G 12G 29% / 4 tmpfs 491M 228K 491M 1% /dev/shm 5 /dev/sda1 283M 28M 240M 11% /boot 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# df -h | grep /dev/sda2 | awk '{print $5}' | cut -d "%" -f 1 8 29 9 [root@yinzhengjie ~]#
b>.awk的BEGIN用法
1 [root@yinzhengjie ~]# cat student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 1 yzj boy 98 96 97 95 96.5 4 2 jay boy 95 98 95 96 96 5 3 ainy girl 90 99 93 98 95 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# awk 'BEGIN{print "yinzhengjie is a good boy!"}{print $2 "\t" $5}' student.txt 9 yinzhengjie is a good boy! 10 Name PHP 11 yzj 96 12 jay 98 13 ainy 99 14 [root@yinzhengjie ~]#
c>awk中的BEGIN配合"FS"内置变量的使用
1 [root@yinzhengjie ~]# awk '{FS=":"}{print $1 "\t" $3}' /etc/passwd | head -5 2 root:x:0:0:root:/root:/bin/bash 3 bin 1 4 daemon 2 5 adm 3 6 lp 4 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# 9 [root@yinzhengjie ~]# awk 'BEGIN{FS=":"}{print $1 "\t" $3}' /etc/passwd | head -5 10 root 0 11 bin 1 12 daemon 2 13 adm 3 14 lp 4 15 [root@yinzhengjie ~]#
d>.awk中的END方法
1 [root@yinzhengjie ~]# awk 'BEGIN{FS=":"}{print $1 "\t" $3}END{print "尹正杰到此一游"}' /etc/passwd | tail -5 2 ldap 55 3 avahi 70 4 nscd 28 5 nslcd 65 6 尹正杰到此一游 7 [root@yinzhengjie ~]#
e>.awk中的关系运算符
1 [root@yinzhengjie ~]# cat student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 1 yzj boy 98 96 97 95 96.5 4 2 jay boy 95 98 95 96 96 5 3 ainy girl 90 99 93 98 95 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# cat student.txt | grep -v Name | awk '$6 >= 95 {print $2}' 9 yzj 10 jay 11 [root@yinzhengjie ~]#
4.sed命令
sed是一种几乎包括在所有UNIX平台(包括Linux)的轻量级流编辑器。sed主要是用来将数据进行选取,替换,删除,新增的命令。

a>.行数据操作
打印操作配合“-n”选项
1 [root@yinzhengjie ~]# cat student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 1 yzj boy 98 96 97 95 96.5 4 2 jay boy 95 98 95 96 96 5 3 ainy girl 90 99 93 98 95 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# sed '2p' student.txt 8 ID Name Sex Golang PHP Linux MySQL Average 9 1 yzj boy 98 96 97 95 96.5 10 1 yzj boy 98 96 97 95 96.5 11 2 jay boy 95 98 95 96 96 12 3 ainy girl 90 99 93 98 95 13 [root@yinzhengjie ~]# 14 [root@yinzhengjie ~]# sed -n '2p' student.txt 15 1 yzj boy 98 96 97 95 96.5 16 [root@yinzhengjie ~]# 17 [root@yinzhengjie ~]# df -h 18 Filesystem Size Used Avail Use% Mounted on 19 /dev/sda2 18G 4.8G 12G 29% / 20 tmpfs 491M 228K 491M 1% /dev/shm 21 /dev/sda1 283M 28M 240M 11% /boot 22 [root@yinzhengjie ~]# 23 [root@yinzhengjie ~]# df -h | sed -n '2p' 24 /dev/sda2 18G 4.8G 12G 29% / 25 [root@yinzhengjie ~]# 26 [root@yinzhengjie ~]#
b>.删除操作,但是并不影响原文件
1 [root@yinzhengjie ~]# cat student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 1 yzj boy 98 96 97 95 96.5 4 2 jay boy 95 98 95 96 96 5 3 ainy girl 90 99 93 98 95 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# sed '1,3d' student.txt 8 3 ainy girl 90 99 93 98 95 9 [root@yinzhengjie ~]# 10 [root@yinzhengjie ~]# cat student.txt 11 ID Name Sex Golang PHP Linux MySQL Average 12 1 yzj boy 98 96 97 95 96.5 13 2 jay boy 95 98 95 96 96 14 3 ainy girl 90 99 93 98 95 15 [root@yinzhengjie ~]#
c>.追加操作
1 [root@yinzhengjie ~]# sed '2a yinzhengjie' student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 1 yzj boy 98 96 97 95 96.5 4 yinzhengjie 5 2 jay boy 95 98 95 96 96 6 3 ainy girl 90 99 93 98 95 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# 9 [root@yinzhengjie ~]# 10 [root@yinzhengjie ~]# cat student.txt 11 ID Name Sex Golang PHP Linux MySQL Average 12 1 yzj boy 98 96 97 95 96.5 13 2 jay boy 95 98 95 96 96 14 3 ainy girl 90 99 93 98 95 15 [root@yinzhengjie ~]#
d>.插入操作:
1 [root@yinzhengjie ~]# sed '2i hello \ 2 golang' student.txt 3 ID Name Sex Golang PHP Linux MySQL Average 4 hello 5 golang 6 1 yzj boy 98 96 97 95 96.5 7 2 jay boy 95 98 95 96 96 8 3 ainy girl 90 99 93 98 95 9 [root@yinzhengjie ~]# 10 [root@yinzhengjie ~]# cat student.txt 11 ID Name Sex Golang PHP Linux MySQL Average 12 1 yzj boy 98 96 97 95 96.5 13 2 jay boy 95 98 95 96 96 14 3 ainy girl 90 99 93 98 95 15 [root@yinzhengjie ~]#
e>.替换操作
1 [root@yinzhengjie ~]# sed '2c 尹正杰' student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 尹正杰 4 2 jay boy 95 98 95 96 96 5 3 ainy girl 90 99 93 98 95 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# cat student.txt 9 ID Name Sex Golang PHP Linux MySQL Average 10 1 yzj boy 98 96 97 95 96.5 11 2 jay boy 95 98 95 96 96 12 3 ainy girl 90 99 93 98 95 13 [root@yinzhengjie ~]#
f>.修改文件操作
1 [root@yinzhengjie ~]# sed '2s/95/100/g' student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 1 yzj boy 98 96 97 100 96.5 4 2 jay boy 95 98 95 96 96 5 3 ainy girl 90 99 93 98 95 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# cat student.txt 8 ID Name Sex Golang PHP Linux MySQL Average 9 1 yzj boy 98 96 97 95 96.5 10 2 jay boy 95 98 95 96 96 11 3 ainy girl 90 99 93 98 95 12 [root@yinzhengjie ~]# 13 [root@yinzhengjie ~]# sed -i '2s/95/100/g' student.txt 14 [root@yinzhengjie ~]# 15 [root@yinzhengjie ~]# cat student.txt 16 ID Name Sex Golang PHP Linux MySQL Average 17 1 yzj boy 98 96 97 100 96.5 18 2 jay boy 95 98 95 96 96 19 3 ainy girl 90 99 93 98 95 20 [root@yinzhengjie ~]#
g>.多条sed命令用法展示
1 [root@yinzhengjie ~]# cat student.txt 2 ID Name Sex Golang PHP Linux MySQL Average 3 1 yzj boy 98 96 97 100 96.5 4 2 jay boy 95 98 95 96 96 5 3 ainy girl 90 99 93 98 95 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# sed -e 's/jay//g;s/ainy//g' student.txt 8 ID Name Sex Golang PHP Linux MySQL Average 9 1 yzj boy 98 96 97 100 96.5 10 2 boy 95 98 95 96 96 11 3 girl 90 99 93 98 95 12 [root@yinzhengjie ~]# 13 [root@yinzhengjie ~]#
四.字符处理命令
1.排序命令sort

a>.按照字母顺序排序
1 [root@yinzhengjie ~]# sort /etc/passwd |head -5 2 abrt:x:173:173::/etc/abrt:/sbin/nologin 3 adm:x:3:4:adm:/var/adm:/sbin/nologin 4 apache:x:48:48:Apache:/var/www:/sbin/nologin 5 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin 6 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin 7 [root@yinzhengjie ~]#
b>.按照字母顺序反序排序
1 [root@yinzhengjie ~]# sort -r /etc/passwd |head -5 2 yinzhengjie:x:500:500:yinzhengjie:/home/yinzhengjie:/bin/bash 3 vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin 4 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 5 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin 6 tcpdump:x:72:72::/:/sbin/nologin 7 [root@yinzhengjie ~]#
c>.根据数字的大小进行排序
1 [root@yinzhengjie ~]# sort -t ":" -k 3,3 /etc/passwd | head -5 2 root:x:0:0:root:/root:/bin/bash 3 bin:x:1:1:bin:/bin:/sbin/nologin 4 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 5 operator:x:11:0:operator:/root:/sbin/nologin 6 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# 9 [root@yinzhengjie ~]# sort -n -t ":" -k 3,3 /etc/passwd | head -5 10 root:x:0:0:root:/root:/bin/bash 11 bin:x:1:1:bin:/bin:/sbin/nologin 12 daemon:x:2:2:daemon:/sbin:/sbin/nologin 13 adm:x:3:4:adm:/var/adm:/sbin/nologin 14 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 15 [root@yinzhengjie ~]# 16 [root@yinzhengjie ~]#
2.统计命令wc

a>.统计行数
1 [root@yinzhengjie ~]# wc -l /etc/passwd 2 37 /etc/passwd 3 [root@yinzhengjie ~]# 4 [root@yinzhengjie ~]# df -h | wc -l 5 4 6 [root@yinzhengjie ~]#
b>.统计单词个数以及字符数
1 [root@yinzhengjie ~]# wc -l /etc/passwd 2 37 /etc/passwd 3 [root@yinzhengjie ~]# wc -w /etc/passwd 4 57 /etc/passwd 5 [root@yinzhengjie ~]# 6 [root@yinzhengjie ~]# wc -m /etc/passwd 7 1757 /etc/passwd 8 [root@yinzhengjie ~]#