D17-操作系统正则符号
2019-07-28 本文已影响0人
荆俊玮
一、补充
1.find -ls
系统文件属性 (文件类型 权限信息 用户信息 inode/block 硬链接和软链接)
find -ls --- 找到数据之后显示数据的属性信息
二、系统通配符号:
1.* --- 匹配所有内容
ls -l /oldboy/*.txt --- 匹配以.txt结尾信息进行详细显示
rm -rf /oldboy/* --- 删除oldboy目录中所有信息, 但是默认无法删除隐藏文件
rm -f /oldboy/.old*.txt --- 删除以old开头 .txt结尾的所有隐藏文件
find /oldboy -name "*.txt"
find /oldboy -name "oldboy*"
find /oldboy -name "old*.txt"
2.{} --- 匹配序列信息
数字序列:
字母序列:
组合序列:
echo A{,2} --- A A2
/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 /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
cp messages{-20190707,}
三、系统正则符号: grep sed awk
基础正则: basic regular expression BRE
扩展正则: extended regular expression ERE
a.基础正则:
1.^ --- 以什么开头的信息
# ll /etc/|grep "^d"
drwxr-xr-x. 3 root root 101 Jul 3 20:25 abrt
drwxr-xr-x. 2 root root 252 Jul 17 18:13 alternatives
drwxr-x---. 3 root root 43 Jul 3 20:26 audisp
2.$ --- 以什么结尾的信息
# grep 'd$' /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
说明: 如果结尾有空格信息, 过滤是会出现异常
利用底行模式 :set list 可以查看结尾空格信息
利用命令查看 cat -A /etc/selinux/config
问题: 将以点结尾的信息过滤出来:
grep '\.$' /etc/selinux/config
# This file controls the state of SELinux on the system.
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings insteadof enforcing.
# disabled- No SELinux policy is loaded.
# minimum - Modification of targetedpolicy. Only selectedprocesses are protected.
# mls - Multi Level Security protection.
3.\ (不属于正则) --- 转义符号
01. 将有特殊意义的符号, 还原本身符号意义
02. 将没有意义的字符,变为有意义
\r \n --- 换行符
\t --- 指表符
03. 取消别名功能
[root@oldboyedu ~]# echo -e "oldboy01\noldboy02" >oldboy.txt
[root@oldboyedu ~]# cat oldboy.txt
oldboy01
oldboy02
4.^$ --- 空行信息
grep -v "^$" /etc/selinux/config
. --- 匹配任意一个且只有一个字符
grep "^." /etc/selinux/config --- 匹配任意一个以什么开头的字符
grep ".$" /etc/selinux/config --- 匹配任意一个以什么结尾的字符
grep "." /etc/selinux/config --- 匹配整个文件所有任意字符
总结匹配筛选文件信息:
01. 会按照行进行匹配
02. 会匹配信息贪婪特性
03. 利用grep -o可以查看匹配信息的过程
5.* --- 匹配前一个字符连续出现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!
匹配文件中数字0信息
[root@oldboyedu oldboy]# grep "0*" oldboy.txt
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@oldboyedu oldboy]# grep -o "0*" oldboy.txt
000
00000
6..* --- 匹配文件中所有信息
[root@oldboyedu oldboy]# grep ".*" oldboy.txt -o
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!
7.[] --- 匹配中括号中任意一个字符
[root@oldboyedu oldboy]# grep "ba" oldboy.txt --- 表示过滤一个字符串
I like badminton ball ,billiard ball and chinese chess!
[root@oldboyedu oldboy]# grep "[bac]" oldboy.txt
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 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忽略大小写进行过滤
8.[^] --- 将指定字符信息,进行取反过滤
[root@oldboyedu oldboy]# grep -v "[a-Z]" oldboy.txt
[root@oldboyedu oldboy]# grep "[^a-Z]" oldboy.txt
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!
b.系统扩展正则 (高级符号) grep --> egrep/grep -E sed --> sed -r
1.+ --- 匹配前一个字符连续出现1次或者多次
# grep "0*" oldboy.txt
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!
# egrep "0+" oldboy.txt
our site is 0 http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
2.| --- 匹配多个字符串信息
匹配oldboy和oldbey单词信息
grep "oldb[oe]y" oldboy.txt
grep "oldb.y" oldboy.txt
egrep "oldboy|oldbey" oldboy.txt
企业应用: 配置文件有注释有空行, 如何进行取消
grep -vE "^$|^#" /etc/services
3.() --- 将多个字符信息进行整合过滤 难点
实现后项引用前项进行替换 常用
第一个作用实践说明
[root@oldboyedu oldboy]# grep [old] oldboy.txt
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 0 http://www.etiantian.org
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu oldboy]# egrep "(old)" oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
第二个作用: 配合sed命令使用时,可以实现后项引用前项
echo "123456"|sed -r 's#([0-9]+)#<\1>#g'
<123456>
echo "1234567890"|sed -r 's#([0-9])#<\1>#g'
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.
问题:
echo "1234567890"|sed -r 's#(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)#<\1><\2><\3><\4><\5><\6><\7><\8><\9><(\10)>#g'
<1><2><3><4><5><6><7><8><9><(10)>
4.{} 匹配前一个字符连续出现指定的次数
{n,m} --- 匹配前一个字符连续出现至少n次 最多m次
[root@oldboyedu oldboy]# grep -E "0{2,4}" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu oldboy]# grep -E "0{2,4}" oldboy.txt -o
000
0000
[root@oldboyedu oldboy]# grep -E "0{1,3}" oldboy.txt -o
0
000
000
00
[root@oldboyedu oldboy]# grep -E "0{1,3}" oldboy.txt
our site is 0 http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
{n} --- 匹配前一个字符连续出现正好n次
[root@oldboyedu oldboy]# grep -E "0{3}" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu oldboy]# grep -E "0{2}" oldboy.txt
my qq num is 49000448.
not 4900000448.
{n,} --- 匹配前一个字符连续出现最少n次, 最多不限
{,m} --- 匹配前一个字符连续出现最少0次, 最多是m次
[root@oldboyedu oldboy]# grep -E "0{3,}" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu oldboy]# grep -E "0{,3}" oldboy.txt
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 0 http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
5.? --- 匹配前一个字符连续出现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
总结:
通配符号: 匹配文件名称信息
正则符号: 匹配文件内容信息