1. 什么是正则表达式
正则表达式(Regular Expression、regex或regexp,缩写为RE),也译为正规表示法、常规表示法,是一种字符模式,用于在查找过程中匹配指定的字符。
支持正则表达式的程序如:locate |find| vim| grep| sed |awk
1.1 第一类正则
元字符: . * ?
前导字符: abc* aooo.
字符 | 用法 |
---|---|
. | 点 任意单个字符 |
* | 前导字符出现0次或连续的多次 |
.* | 任意长度的字符 |
^ | 开头 |
$ | 结尾 |
^$ | 空行 |
[ ] | 匹配指定字符串内的任意单个字符 |
[^] | 匹配不在的单个字符 |
^[ ] | 匹配以指定字符组内的任一字符开头 |
^[^] | 匹配不以指定字符组内的任一字符开头 |
< | 取单词的头 |
> | 取单词的尾 |
<> | 精确匹配符号 |
{n} | 匹配前导字符连续出现n次 |
{n,} | 匹配前导字符至少出现n次 |
{n,m} | 匹配前导字符出现n次与m次之间 |
(strings) | 保存被匹配的字符 |
[0-9] [a-z] [A-Z] [a-zA-Z] [a-Z]
Perl内置正则:
\d 匹配数字 [0-9]
\w 匹配字母数字下划线[a-zA-Z0-9_]
\s 匹配空格、制表符、换页符[\t\r\n]
grep -P '\d' test.txt
grep -P '\w' test.txt
grep -P '\s' test.txt
扩展类的正则表达式 grep -E 或则 egrep
1.2 扩展正则
+ | 匹配一个或多个前导字符 |
---|---|
? | 匹配零个或一个前导字符 |
a | b |
() | 组字符 hello myself yourself (my |
{n} | 前导字符重复n次 |
{n,} | 前导字符重复至少n次 |
{n,m} | 前导字符重复n到m次 |
egrep "root|ftp|adm" /etc/passwd
grep '[0-9]\{2\}\.[0-9]\{1\}\.[0-9]\{1\}\.[0-9]\{1\}' 1.txt
2. 练习
# cat test.txt
Aieur45869Root0000
9h847RkjfkIIIhello
rootHllow88000dfjj
8ikuioerhfhupliooking
hello world
192.168.0.254
welcome to uplooking.
abcderfkdjfkdtest
rlllA899kdfkdfj
iiiA848890ldkfjdkfj
abc
12345678908374
123456@qq.com
123456@163.com
abcdefg@itcast.com23ed
1、查找不以大写字母开头的行(三种写法)。
grep -v '^[A-Z]' cat.txt
egrep '^[a-z]|^[0-9]' cat.txt
2、查找有数字的行(两种写法)
grep '[0-9]' cat.txt
grep -P '\d' cat.txt
3、查找一个数字和一个字母连起来的
egrep '[0-9][a-zA-Z]' cat.txt
4、查找不以r开头的行
grep -v '^r' cat.txt
5、查找以数字开头的
grep '^[0-9]' cat.txt
6、查找以大写字母开头的
egrep '^[A-Z]' cat.txt
7、查找以小写字母开头的
egrep '^[a-z]' cat.txt
8、查找以点结束的
grep '\.$' cat.txt
9、去掉空行
grep -v '^$' cat.txt
10、查找完全匹配abc的行
11、查找A后有三个数字的行
egrep 'A[0-9]{3}' cat.txt
12、统计root在/etc/passwd里出现了几次
13、用正则表达式找出自己的IP地址、广播地址、子网掩码
ifconfig eth0|grep broad|egrep -o '([0-9]{3}\.)([0-9]{3}\.)([0-9]{1,3}\.)([0-9]{1,3})'
14.找出文件中的ip地址并且打印替换成172.16.2.254
egrep -o '([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})' cat.txt |sed -n 's/192.168.0.\(254\)/172.168.0.\1/p'
15.找出文件中的ip地址
egrep '([0-9]{1,3}\.){3}[0-9]{1,3}' cat.txt
17、找出邮箱地址
grep -E '^[0-9]+@[a-z0-9]+\.[a-z]+$'
3. 案例
写一个自动搭建apache服务的脚本,要求如下:
1、用户输入web服务器的IP、域名以及数据根目录
2、如果用户不输入则一直提示输入,直到输入为止
3、当访问www.test.cc时可以访问到数据根目录里的首页文件“this is test page”
#!/bin/bash
conf=/etc/httpd/conf/httpd.conf
input_fun()
{
input_var=""
output_var=$1
while [ -z $input_var ]
do
read -p "$output_var" input_var
done
echo $input_var
}
ipaddr=$(input_fun "Input Host ip[192.168.0.1]:")
web_host_name=$(input_fun "Input VirtualHostName [www.test.cc]:")
root_dir=$(input_fun "Input host Documentroot dir:[/var/www/html]:")
[ ! -d $root_dir ] && mkdir -p $root_dir
chown apache.apache $root_dir && chmod 755 $root_dir
echo this is $web_host_name > $root_dir/index.html
echo "$ipaddr $web_host_name" >> /etc/hosts
[ -f $conf ] && cat >> $conf <<end
NameVirtualHost $ipaddr:80
<VirtualHost $ipaddr:80>
ServerAdmin webmaster@$web_host_name
DocumentRoot $root_dir
ServerName $web_host_name
ErrorLog logs/$web_host_name-error_log
CustomLog logs/$web_host_name-access_loh common
</VirtualHost>
end