(RegularExpression,RE)
又称
代码中常简写为 regex、regexp 或 RE
正则表达式用途
- [root@localhost ~]# cat test.txt
- he was short and fat.
- he was weating a blue polo shirt with black pants.
- The home of Football on BBC Sport online.
- the tongue is boneless but it breaks bones.12!
- google is the best tools for search keyword.
- PI=3.14
- a wood cross!
- Actions speak louder than words
-
- #woood #
- #woooooooood #
- AxyzxyzxyzxyzC
- I bet this place is really spooky late at night!
- Misfortunes never come alone/single.
- I shouldn't have lett so tast.
查看包含 the 的行
- [root@localhost ~]# grep -ni 'the' test.txt
- 3:The home of Football on BBC Sport online.
- 4:the tongue is boneless but it breaks bones.12!
- 5:google is the best tools for search keyword.
注
-n:显示行号
-i:不区分大小写
-v:不包含指定字符
利用 [] 查找集合字符
- [root@localhost ~]# grep -n 'sh[io]rt' test.txt
- 1:he was short and fat.
- 2:he was weating a blue polo shirt with black pants.
注
[ ]:中括号内不管写几个字符,都只匹配一个,表示匹配其中的任何一个字符
查找字母 oo 前不是字母 w 的内容
- [root@localhost ~]# grep -n '[^w]oo' test.txt
- 3:The home of Football on BBC Sport online.
- 5:google is the best tools for search keyword.
- 9:#woood #
- 10:#woooooooood #
- 12:I bet this place is really spooky late at night!
查看字母 oo 前不是小写字母的内容
- [root@localhost ~]# grep -n '[^a-z]oo' test.txt
- 3:The home of Football on BBC Sport online.
(注:字母前直接用^,表示取反 [ ] 前用^,表示以括号中的字符开头)
查看以 the 为行首的行
- [root@localhost ~]# grep -n '^the' test.txt
- 4:the tongue is boneless but it breaks bones.12!
查询以小写字母开头的行
- [root@localhost ~]# grep -n '^[a-z]' test.txt
- 1:he was short and fat.
- 2:he was weating a blue polo shirt with black pants.
- 4:the tongue is boneless but it breaks bones.12!
- 5:google is the best tools for search keyword.
- 7:a wood cross!
查询以大写字母开头的行
- [root@localhost ~]# grep -n '^[A-Z]' test.txt
- 3:The home of Football on BBC Sport online.
- 6:PI=3.14
- 8:Actions speak louder than words
- 11:AxyzxyzxyzxyzC
- 12:I bet this place is really spooky late at night!
- 13:Misfortunes never come alone/single.
- 14:I shouldn't have lett so tast.
查看以非字母开头的行
- [root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
- 9:#woood #
- 10:#woooooooood #
查看以点结尾的行
- [root@localhost ~]# grep -n '\.$' test.txt
- 1:he was short and fat.
- 2:he was weating a blue polo shirt with black pants.
- 3:The home of Football on BBC Sport online.
- 5:google is the best tools for search keyword.
- 13:Misfortunes never come alone/single.
- 14:I shouldn't have lett so tast.
查询空行
[root@localhost ~]# grep -n '^$' test.txt
查询非空行
[root@localhost ~]# grep -n -v '^$' test.txt
查找包含四字符的单词的行,单词以w开头,以d结尾
- [root@localhost ~]# grep -n 'w..d' test.txt
- 5:google is the best tools for search keyword.
- 7:a wood cross!
- 8:Actions speak louder than words
(注:一个点代表一个字符)
查询至少包含两个字母o(oo)字符串的行
- [root@localhost ~]# grep -n 'ooo*' test.txt
- 3:The home of Football on BBC Sport online.
- 5:google is the best tools for search keyword.
- 7:a wood cross!
- 10:#woood #
- 11:#woooooooood #
- 13:I bet this place is really spooky late at night!
(注:ooo*:前两个o是条件,表示包含两个o;然后是o*,表示后面有零个或多个重复o)
查找行,行中单词包含w开头和d结尾,中间至少一个字母o
- [root@localhost ~]# grep -n 'woo*d' test.txt
- 7:a wood cross!
- 10:#woood #
- 11:#woooooooood #
查询以w开头,d结尾,中间字符可有可无
- [root@localhost ~]# grep -n 'w.*d' test.txt
- 1:he was short and fat.
- 5:google is the best tools for search keyword.
- 7:a wood cross!
- 8:Actions speak louder than words
- 10:#woood #
- 11:#woooooooood #
查询包含数字的行
- [root@localhost ~]# grep -n '[0-9][0-9]*' test.txt
- 4:the tongue is boneless but it breaks bones.12!
- 6:PI=3.14
查询包含两个o的字符
- [root@localhost ~]# grep -n 'o\{2\}' test.txt
- 3:The home of Football on BBC Sport online.
- 5:google is the best tools for search keyword.
- 7:a wood cross!
- 10:#woood #
- 11:#woooooooood #
- 13:I bet this place is really spooky late at night!
(注:'o\{2\}':表示两个字母o)
w开头,d结尾中间有2--5个o
- [root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
- 7:a wood cross!
- 10:#woood #
w开头,d结尾中间有2个以上o
- [root@localhost ~]# grep -n 'wo\{2,\}d' test.txt
- 7:a wood cross!
- 10:#woood #
- 11:#woooooooood #
[root@localhost ~]# grep -v '^$' /etc/ssh/sshd_config | grep -v '^#'
用扩展正则表达式表示
基础正则表达式包含两个定位元字符
好处
Shell编程三剑客
sed 命令两种格式
- sed [选项] '操作' 参数
- sed [选项] -f scriptfile 参数
“参数”指操作的目标文件,当存在多个操作对象时用,文件之间用逗号“,”分隔
scriptfile表示脚本文件,需要用“-f”选项指定,当脚本文件出现在目标文件之前时,表示通过指定的脚本文件来处理输入的目标文件
sed 常用选项
输出符合条件的文本(p 表示正常输出)
- 输出所有内容
- [root@localhost ~]# sed -n 'p' test.txt
-
- 输出第三行
- [root@localhost ~]# sed -n '3p' test.txt
-
- 输出3~5行
- [root@localhost ~]# sed -n '3,5p' test.txt
-
- 输出所有奇数行
- [root@localhost ~]# sed -n 'p;n' test.txt
-
- 输出所有偶数行
- [root@localhost ~]# sed -n 'n;p' test.txt
-
- 输出第1~5行之间的奇数行
- [root@localhost ~]# sed -n '1,5{p;n}' test.txt
-
- 输出第10行至文件尾之间的偶数行
- [root@localhost ~]# sed -n '10,${n;p}' test.txt
- 注释:此命令中,读取的第一行是文件的第10行,读取的第二行,是文件的第11行,依次类推
-
- 输出包含the的行
- [root@localhost ~]# sed -n '/the/p' test.txt
-
- 输出从第4行开始至第一个包含the的行
- [root@localhost ~]# sed -n ' 4,/the/p' test.txt
-
- 输出包含the的行所在的行号
- [root@localhost ~]# sed -n '/the/=' test.txt
-
- 注释:=用来输出行号
-
- 输出以PI开头的行
- [root@localhost ~]# sed -n '/^PI/p' test.txt
-
- 输出包含单词wood的行
- [root@localhost ~]# sed -n '/\
/p' test.txt
删除符合条件的文本(d)
- // 删除第3行
- [root@localhost ~]# nl test.txt | sed '3d' ##显示行号
- 或
- [root@localhost ~]# sed '3d' test.txt ##不显示行号
-
-
- // 删除3~5行
- [root@localhost ~]# nl test.txt |sed '3,5d'
-
- // 删除包含cross的行
- [root@localhost ~]# nl test.txt |sed '/cross/d'
-
- 注释:删除不包含cross的行
- [root@localhost ~]# nl test.txt |sed '/cross/! d'
-
- // 删除以小写字母开头的行
- [root@localhost ~]# sed '/^[a-z]/d' test.txt
-
- // 删除以点结尾的行
- [root@localhost ~]# sed '/\.$/d' test.txt
-
- // 删除空行
- [root@localhost ~]# sed '/^$/d' test.txt
替换符合条件的文本
- 将每行的第一个the换成THE
- [root@localhost ~]# sed 's/the/THE/' test.txt
-
- 将每行中的第2个l换成L
- [root@localhost ~]# sed 's/l/L/2' test.txt
-
- 将文中所有的the换成THE
- [root@localhost ~]# sed 's/the/THE/g' test.txt
-
- 将文中所有的o删除
- [root@localhost ~]# sed 's/o//g' test.txt
-
- 在每行的行首插入#
- [root@localhost ~]# sed 's/^/#/' test.txt
- 注释:在每行行尾添加#
- [root@localhost ~]# sed 's/$/#/' test.txt
-
- 在包含the的每行的行首插入#
- [root@localhost ~]# sed '/the/s/^/#/' test.txt
-
- 在每行的行尾插入字符串EOF
- [root@localhost ~]# sed 's/$/EOF/' test.txt
-
- 将第3~5行中的所有the替换成THE
- [root@localhost ~]# sed '3,5s/the/THE/g' test.txt
-
- 将包含the的所有行中的o都替换成O
- [root@localhost ~]# sed '/the/s/o/O/g' test.txt
迁移符合条件的文本
- // 将包含the的行迁移至文件的末尾
- [root@localhost ~]# sed '/the/{H;d};$G' test.txt
-
- // 将第1~5行的内容转移至第17行后
- [root@localhost ~]# sed '1,5{H;d};17G' test.txt
-
- // 将包含the的行另存为文件out.txt
- [root@localhost ~]# sed '/the/w out.txt' test.txt
-
- // 将文件/etc/hostname的内容添加到包含the的每一行后
- [root@localhost ~]# sed '/the/r /etc/hostname' test.txt
-
- // 在第3行后插入一个新行,内容为#chkconfig:35 82 20
- [root@localhost ~]# sed '3a#chkconfig:35 82 20' test.txt
-
- // 在包含the的每行后插入一个新行,内容为New
- [root@localhost ~]# sed '/the/aNew' test.txt
-
- // 在第3行后插入多行内容
- [root@localhost ~]# sed '3aNew1\nNew2' test.txt
-
- 注释:\n为换行,添加两行为New1和New2
使用脚本编辑文件
- //将第 1~5 行内容转移至第 17 行后
- sed '1,5{H;d};17G' test.txt
- [root@localhost ~]# vi opt.list
- 1,5H
- 1,5d
- 17G
- [root@localhost ~]# sed -f opt.list test.txt
- The year ahead will test our political establishment to the limit. PI=3.141592653589793238462643383249901429
- a wood cross!
- Actions speak louder than words
- #woood #
- #woooooood #
- AxyzxyzxyzxyzC
- I bet this place is really spooky late at night!
- Misfortunes never come alone/single.
- I shouldn't have lett so tast. he was short and fat.
- He was wearing a blue polo shirt with black pants.
- The home of Football on BBC Sport online.
- the tongue is boneless but it breaks bones.12!
- google is the best tools for search keyword.
sed 直接操作文件
- [root@localhost ~]# vim local_only_ftp.sh
- #!/bin/bash
- # 指定样本文件路径、配置文件路径
- SAMPLE="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf " CONFIG="/etc/vsftpd/vsftpd.conf"
- # 备份原来的配置文件,检测文件名为/etc/vsftpd/vsftpd.conf.bak 备份文件是否存在, 若不存在则使用 cp 命令进行文件备份
- [ ! -e "$CONFIG.bak" ] && cp $CONFIG $CONFIG.bak
- # 基于样本配置进行调整,覆盖现有文件
- sed -e '/^anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIG
- sed -i -e '/^local_enable/s/NO/YES/g' -e '/^write_enable/s/NO/YES/g' $CONFIG
- grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG
- # 启动 vsftpd 服务,并设为开机后自动运行
- systemctl restart vsftpd
- systemctl enable vsftpd
- [root@localhost ~]# chmod +x local_only_ftp.sh

awk 常见用法
awk案例
按行输出文本
- //输出所有
- awk -F":" '{print}' /etc/passwd
- //输出所有
- awk -F":" '{print $0}' /etc/passwd
- //显示第3行到第6行
- awk -F: 'NR==3,NR==6{print}' /etc/passwd
- //显示第3行到第6行
- awk -F: 'NR>=3&&NR<=6{print}' /etc/passwd
- //显示第3行和第6行
- awk -F: 'NR==3||NR==6{print}' /etc/passwd
- //显示奇数行
- awk '(NR%2)==1{print}' /etc/passwd
- //显示偶数行
- awk '(NR%2)==0{print}' /etc/passwd
- //显示以root开头的行
- awk '/^root/{print}' /etc/passwd
- //显示以nologin结尾的行
- awk '/nologin$/{print}' /etc/passwd
- //统计以/bin/bash结尾的行数
- awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd
- //统计以空行分隔的文本段落数
- awk 'BEGIN{RS=""};END{print NR}' /etc/ssh/sshd_config
-
- //输出每行的行号
- awk '{print NR,$0}' /etc/passwd
- //依次打印行号,字段数,最后字段值,制表符,每行内容
- awk -F: '{print NR,NF,$NF,"\t",$0}' /etc/passwd
- //显示第5行
- awk -F: 'NR==5{print}' /etc/passwd
- //不显示第一行
- route -n|awk 'NR!=1{print}'
-
- //显示每行有多少字段
- awk -F: '{print NF}' /etc/passwd
- //将每行第NF个字段的值打印出来
-
- awk -F: '{print $NF}' /etc/passwd
- //显示只有4个字段的行
- awk -F: 'NF==4 {print }' /etc/passwd
- //显示每行字段数量大于2的行
- awk -F: 'NF>2{print $0}' /etc/passwd
按字段输出文本
- //显示第三列
- awk -F":" '{print $3}' /etc/passwd
- //$1与$3相连输出,无空格,
- awk -F":" '{print $1 $3}' /etc/passwd
- //多了一个逗号,输出第1和第3个字段,有空格
- awk -F":" '{print $1,$3}' /etc/passwd
- //统计密码为空的shadow记录
- awk -F: '$2=="!!" {print}' /etc/shadow
- ##显示密码为空的用户的shadow信息
- awk 'BEGIN {FS=":"}; $2=="!!" {print}' /etc/shadow
- ##显示第七个字段为/bash的行的第一个字段
- awk -F ":" '$7~"/bash" {print $1}' /etc/passwd
- //显示第5行
- awk -F: 'NR==5{print}' /etc/passwd
- //$1与$3之间手动添加空格分隔
- awk -F":" '{print $1 " " $3}' /etc/passwd
通过管道、双引号调用shell命令
- ##统计bash用户的个数
- awk -F: '/bash$/{print | "wc -l"}' /etc/passwd
- ##统计在线用户的数量
- awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}}'
- ##输出当前主机名
- awk 'BEGIN {"hostname" | getline;print $0}'
-
- //逻辑与,$1匹配mail,并且$3>6
- awk -F: '$1~/mail/ && $3>6 {print }' /etc/passwd
- awk -F: '{if($1~/mail/ && $3>8) print }' /etc/passwd
- //逻辑或,统计以mail开头或第3列大于1000的行
- awk -F: '$1~/mail/ || $3>1000 {print }' /etc/passwd
- awk -F: '{if($1~/mail/ || $3>1000) print }' /etc/passwd
优点
awk 包含几个特殊的内建变量(可直接用)
sort 是以行为单位对文件内容进行排序的工具,也可以根据不同的数据类型来排序
sort 命令的语法为“sort [选项] 参数”,其中常用的选项包括
/将/etc/passwd 文件中的账号进行排序
[root@localhost ~]# sort /etc/passwd
将/etc/passwd 文件中第三列进行反向排序
[root@localhost ~]# sort -t ':' -rk 3 /etc/passwd
将/etc/passwd 文件中第三列进行排序,并将输出内容保存至 user.txt 文件中
通常与 sort 命令结合使用,用于报告或者忽略文件中的重复行
命令语法格式为:uniq [选项] 参数
删除 testfile 文件中的重复行
- [root@localhost ~]# cat testfile
- Linux 10
- Linux 20
- Linux 30
- Linux 30
- Linux 30
- CentOS 6.5
- CentOS 6.5
- CentOS 6.5
- CentOS 7.3
- CentOS 7.3
- CentOS 7.3
-
- [root@localhost ~]# uniq testfile
- Linux 10
- Linux 20
- Linux 30
- CentOS 6.5
- CentOS 7.3
常用于标准输入的字符进行替换、压缩和删除
常用选项包括
将输入字符由大写转换为小写
- [root@localhost ~]# echo "KGC" | tr 'A-Z' 'a-z'
- kgc
压缩输入中重复的字符
- [root@localhost ~]# echo "thissss is a text linnnnnnne." | tr -s 'sn'
- this is a text line.
删除字符串中某些字符
- [root@localhost ~]# echo 'hello world' | tr -d 'od'
- hell wrl
小阿轩yx-Shell编程之正则表达式与文本处理器