-
系统通配符号
-
系统正则符号
1.系统通配符号
* ---匹配所有内容
如:ls -l /oldboy/*.txt --- 匹配以.txt结尾信息进行详细显示
rm -rf /oldboy/* --- 删除oldboy目录中所有信息, 但是默认无法删除隐藏文件
rm -f /oldboy/.old*.txt --- 删除以old开头 .txt结尾的所有隐藏文件
{ } --- 匹配序列信息
数字序列:
[root@oldboy63 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
字母序列:
[root@oldboy63 ~]# echo {a..j}
a b c d e f g h i j
组合序列:
[root@oldboy63 ~]# echo A{,2}
A A2
利用此组合序列可实现对路径较深目录快速备份与还原数据:
备份:cp /oldboy/oldboy01/oldboy02/oldboy03/oldboy04/oldboy.txt{,.bak} == cp /oldboy/oldboy01/oldboy02/oldboy03/oldboy04/oldboy.txt /oldboy/oldboy01/oldboy02/oldboy03/oldboy04/oldboy.txt.bak
还原:cp /oldboy/oldboy01/oldboy02/oldboy03/oldboy04/oldboy.txt{.bak,} == cp /oldboy/oldboy01/oldboy02/oldboy03/oldboy04/oldboy.txt.bak /oldboy/oldboy01/oldboy02/oldboy03/oldboy04/oldboy.txt
2.系统正则符号
通常与grep、sed、awk配合使用
基础正则:basic regular expression BRE
扩展正则: extended regular expression ERE
基础正则:
^ :以什么开头的信息
[root@oldboy63 ~]# ll /etc/|grep "^d"
drwxr-xr-x. 3 root root 101 Jul 13 15:27 abrt
drwxr-xr-x. 2 root root 236 Jul 13 15:28 alternatives
drwxr-x---. 3 root root 43 Jul 13 15:28 audisp
$:以什么结尾的信息
[root@oldboy63 ~]# grep 'd$' /etc/selinux/config
SELINUX=disabled
注意:如果结尾有空格信息,过滤是会出现异常
利用底行模式:set list 可以查看结尾空格信息
也可利用命令查看:cat -A /etc/selinux/config
\(不属于正则):转义符号
1.将有特殊意义的符号还原本身符号意义
将以点结尾的信息过滤出来:
[root@oldboy63 ~]# grep "\.$" /etc/selinux/config
# This file controls the state of SELinux on the system.
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# mls - Multi Level Security protection.
2.将没有意义的字符变为有意义字符
如:\r \n ---换行符
3.取消别名功能
^$:空行信息
grep -v "^$" /etc/selinux/config
-v ---过滤
. :匹配任意一个且只有一个字符
grep "^." /etc/selinux/config --- 匹配任意一个以什么开头的字符
grep ".$" /etc/selinux/config --- 匹配任意一个以什么结尾的字符
grep "." /etc/selinux/config --- 匹配整个文件所有任意字符
匹配筛选文件信息:
1.会按照行进行匹配
2.会匹配信息贪婪特性
3.利用grep -o可以查看匹配信息的过程
*:匹配前一个字符连续出现0次或者多次
创建一个测试文件:
创建测试文件:
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[root@oldboy63 ~]# grep -o "4*" oldboy.txt
4
44
4
44
.* :匹配文件中所有信息
[root@oldboy63 ~]# grep ".*" oldboy.txt -o
I am oldboy teacher!11
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[]:匹配中括号中任意一个字符
[root@oldboy63 ~]# grep "[bac]" oldboy.txt
I am oldboy teacher!11
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my god ,i am not oldbey,but OLDBOY!
grep "[a-z]" oldboy.txt --- 过滤小写字母
grep "[A-Z]" oldboy.txt --- 过滤大写字母
grep "[a-zA-Z]" oldboy.txt --- 过滤所有字母
grep "[a-Z]" oldboy.txt --- 过滤所有字母
grep -i "[a-z]" oldboy.txt --- grep忽略大小写进行过滤
[^] :将指定字符信息,进行取反过滤
grep "[^a-Z]" oldboy.txt
系统扩展正则(高级符号)
使用扩展符号时grep ——> egrep / grep -E
sed ——>sed -r
+ :匹配前一个字符出现1次或者多次
[root@oldboy63 ~]# egrep "0+" oldboy.txt
my qq num is 49000448.
not 4900000448.
只会过滤出匹配符合条件的两行数据,其他不符合匹配条件的行不会显示
| :匹配多个字符串信息
egrep "oldboy|oldbey" oldboy.txt
当配置文件有注释有空行,如何进行取消
grep -vE "^$|^#" /etc/services
() : 将多个字符信息进行整合过滤
第一个作用:
[root@oldboy63 ~]# egrep "(old)" oldboy.txt
I am oldboy teacher!11
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
第二个作用:配合sed命令使用时,可以实现后项引用前项
[root@oldboy63 ~]# echo "123456"|sed -r 's#([0-9]+)#<\1>#g'
<123456>
[root@oldboy63 ~]# echo "1234567890"|sed -r 's#([0-9])#<\1>#g'
<1><2><3><4><5><6><7><8><9><0>
[root@oldboy63 ~]# echo "789"|sed -r 's#(.)(.)(.)#<\1><\2><\3>#g'
<7><8><9>
注意:后项引用前项只能引用1-9个小括号
批量创建用户:
[root@oldboyedu oldboy]# seq -w 10|sed -r 's#(.*)#useradd oldboy\1;echo 123456|passwd --stdin oldboy\1#g'
useradd oldboy01;echo 123456|passwd --stdin oldboy01
useradd oldboy02;echo 123456|passwd --stdin oldboy02
useradd oldboy03;echo 123456|passwd --stdin oldboy03
useradd oldboy04;echo 123456|passwd --stdin oldboy04
useradd oldboy05;echo 123456|passwd --stdin oldboy05
useradd oldboy06;echo 123456|passwd --stdin oldboy06
useradd oldboy07;echo 123456|passwd --stdin oldboy07
useradd oldboy08;echo 123456|passwd --stdin oldboy08
useradd oldboy09;echo 123456|passwd --stdin oldboy09
useradd oldboy10;echo 123456|passwd --stdin oldboy10
[root@oldboyedu oldboy]# seq -w 10|sed -r 's#(.*)#useradd oldboy\1;echo 123456|passwd --stdin oldboy\1#g'|bash
Changing password for user oldboy01.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy02.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy03.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy04.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy05.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy06.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy07.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy08.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy09.
passwd: all authentication tokens updated successfully.
Changing password for user oldboy10.
passwd: all authentication tokens updated successfully.
{ } :匹配前一个字符连续出现指定的次数
{n,m}:匹配前一个字符连续出现至少n次 最多m次
[root@oldboy63 ~]# grep -E "0{2,4}" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboy63 ~]# grep -E "0{1,3}" oldboy.txt -o
000
000
00
{n,} :匹配前一个字符连续出现最少n次,最多不限
{,m}:匹配前一个字符连续出现最少0次,最多m次
[root@oldboy63 ~]# grep -E "0{3,}" oldboy.txt -o
000
00000
grep -E "0{,3}" oldboy.txt
I am oldboy teacher!11
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
?:匹配前一个字符连续出现0次或者1次
[root@oldboyedu oldboy]# egrep "o*" test02.txt
gd
god
good
goood
gooood
[root@oldboyedu oldboy]# egrep "o+" test02.txt
god
good
goood
gooood
[root@oldboyedu oldboy]# egrep "o?" test02.txt
gd
god
good
goood
gooood
[root@oldboyedu oldboy]# egrep "o?" test02.txt -o
o
o
o
o
o
o
o
o
o
o