Linux运维63期day17

系统通配符号:

  • --- 匹配所有内容
    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"

{} --- 匹配序列信息
数字序列:
字母序列:
组合序列:
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
终极目标:

  1. 文件test.txt
    张三 110106198803076651
    李四 106106199903076651
    王五 oldboy

    grep -vE "[0-9]+" test.txt

    文件内容做修改
    张三 110106198803076651
    李四 106106199903076651
    王五 10101010100101
    周六 101010101010220101010
    grep -E "[0-9X]{18}" test.txt --- 将身份输入不足18位的进行排除

    如何找出不合理身份证人员信息

如何取出IP地址信息

基础正则:
^ --- 以什么开头的信息
# 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
' /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. \ (不属于正则) --- 转义符号 01. 将有特殊意义的符号, 还原本身符号意义 02. 将没有意义的字符,变为有意义 \r \n --- 换行符 \t --- 指表符 03. 取消别名功能 [root@oldboyedu ~]# echo -e oldboy.txt [root@oldboyedu ~]# cat oldboy.txt oldboy01 oldboy02 ^"> --- 空行信息
grep -v "^$" /etc/selinux/config

(.) --- 匹配任意一个且只有一个字符
grep "^." /etc/selinux/config --- 匹配任意一个以什么开头的字符
grep ".$" /etc/selinux/config --- 匹配任意一个以什么结尾的字符
grep "." /etc/selinux/config --- 匹配整个文件所有任意字符
总结匹配筛选文件信息:
01. 会按照行进行匹配
02. 会匹配信息贪婪特性
03. 利用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!

匹配文件中数字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

(.) --- 匹配文件中所有信息
[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!
[] --- 匹配中括号中任意一个字符
[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忽略大小写进行过滤
[^] --- 将指定字符信息,进行取反过滤
[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!

系统扩展正则 (高级符号) grep --> egrep/grep -E sed --> sed -r
+ --- 匹配前一个字符连续出现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.
| --- 匹配多个字符串信息
匹配oldboy和oldbey单词信息
grep "oldb[oe]y" oldboy.txt
grep "oldb.y" oldboy.txt
egrep "oldboy|oldbey" oldboy.txt
企业应用: 配置文件有注释有空行, 如何进行取消
grep -vE "$|#" /etc/services
() --- 将多个字符信息进行整合过滤 难点
实现后项引用前项进行替换 常用
第一个作用实践说明
[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个小括号

{} 匹配前一个字符连续出现指定的次数
{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!
(?) --- 匹配前一个字符连续出现0次或者1次
总结: * + {} ?
[root@oldboyedu oldboy]# egrep "o*" test02.txt
gd
god
good
goood
gooood
[root@oldboyedu oldboy]# egrep "o+" test02.txt
god
good
goood
[root@oldboyedu oldboy]# egrep "o?" test02.txt
gd
god
good
[root@oldboyedu oldboy]# egrep "o?" test02.txt -o
o
o
o
o

你可能感兴趣的:(Linux运维63期day17)