2019-07-23 正则符号

Linux运维学习 Day17

操作系统正则符号

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"

{}  --- 匹配序列信息
数字序列:{1..10}
字母序列:{a..z}  {A-Z}
组合序列:
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,}

2.系统正则符号: grep,sed,awk

基础正则: basic    regular expression  BRE 
扩展正则: extended regular expression  ERE

举例:现有文件test.txt ,内容如下
   张三   110106198803076651
   李四   106106199903076651
   王五   oldboy

   输入: grep -vE "[0-9]+" test.txt 
   过滤出无用信息: 王五   oldboy

   文件内容做修改:
   张三   110106198803076651
   李四   106106199903076651
   王五   10101010100101
   周六   101010101010220101010

   输入:grep -E "[0-9X]{18}" test.txt   --- 将身份输入不足18位的进行排除

3.基础正则

① ^   --- 以什么开头的信息
    # 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

② $   --- 以什么结尾的信息
    # grep 'd$' /etc/selinux/config
    SELINUX=disabled
    SELINUXTYPE=targeted
    说明: 如果结尾有空格信息, 过滤是会出现异常
          利用底行模式 :set list 可以查看结尾空格信息
          利用命令查看 cat -A /etc/selinux/config

    注: ^$  --- 空行信息     输入grep -v "^$" /etc/selinux/config 过滤空行

③ .   --- 匹配任意一个且只有一个字符
    grep "^." /etc/selinux/config    --- 匹配任意一个以什么开头的字符
    grep ".$" /etc/selinux/config    --- 匹配任意一个以什么结尾的字符
    grep "." /etc/selinux/config     --- 匹配整个文件所有任意字符
    总结匹配筛选文件信息:
    01. 会按照行进行匹配
    02. 会匹配信息贪婪特性
    03. 利用grep -o可以查看匹配信息的过程

④  *   --- 匹配前一个字符连续出现0次或者多次
⑤  [ ]  --- 匹配中括号中任意一个字符
   grep "ba" oldboy.txt     --- 表示过滤一个字符串

   过滤出所有小写字母:
   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  --- 将 oldboy.txt 文件中,所有不是字母的内容体现出来
    grep -v "[a-Z]" selinux/config --- 这种写法是错误的示范

⑥ +  --- 匹配前一个字符连续出现1次或者多次
   grep "0*" oldboy.txt = egrep "0+" oldboy.txt 

⑦ |   --- 匹配多个字符串信息
    匹配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个小括号
    
    解锁: 批量创建用户 (密码是随机密码)
    [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.

系统扩展正则 (高级符号) :
① grep --> egrep/grep -E
② sed --> sed -r

你可能感兴趣的:(2019-07-23 正则符号)