• Linux的正则练习


    grep和 egrep的正则表达式

    1、显示三个用户root、wang的UID和默认shell

    cat /etc/passwd | grep “^(root|wang)” | tr ‘:’ ‘ ‘| cut -d’ ‘ -f1,3,7

    cat /etc/passwd | egrep ‘^(root|wang)’ | tr ‘:’ ‘ ‘ | cut -d’ ‘ -f1,3,7

    2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

    cat /etc/rc.d/init.d/functions | grep  “^[[:alpha:]][[:alnum:]_]* *()”
    3、使用egrep取出/etc/rc.d/init.d/functions中其基名

    echo /etc/rc.d/init.d/functions | egrep -o ‘<[[:alnum:]_.]*>/?$'(标准的目录名,字母数字下划线,其他字符不支持)

    echo /etc/rc.d/init.d/functions | egrep -o ‘[^/]+*/?$’

    4、使用egrep取出上面路径的目录名

    echo /etc/rc.d/init.d/functions | egrep -o ‘<[[:alnum:]_.]*>'(标准的目录名,字母数字下划线,其他字符不支持)

    echo /etc/rc.d/init.d/functions | egrep -o ‘[^/]+’
    5、统计last命令中以root登录的每个主机IP地址登录次数

    last | grep ^root | grep ‘[[:digit:]]{1,3}(.[[:digit:]]{1,3}){3}’ | tr -s ‘ ‘ ‘:’ | cut -d: -f1,3 | sort -t”:” -k2 | uniq -c
    6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

    0-9 regex: [[:digit:]]

    10-99 regex: [1-9][0-9]

    100-199 regex: 1[0-9][0-9]

    200-249 regex: [1-2][0-4][0-9]

    250-255 regex: 25[0-5]
    7、显示ifconfig命令结果中所有IPv4地址

    ifconfig | grep -o ‘[[:digit:]]{3}(.[[:digit:]]{3}){3}’
    8、将此字符串:welcome to linux 中的每个字符去重并排序,重复次数多的排到前面

    echo ‘welcome to  linux’ | grep -o “.” | sort | uniq -c | sort -rnk1
    9、用正则表达式表示出QQ号

    qq:  [[:digit:]]{1,12}

    10、用正则表达式表示出身份证号

    15位的身份证号码: 
    
    (1)1~6位为地区代码 
    (2)7~8位为出生年份(2位),9~10位为出生月份,11~12位为出生日期 
    (3)第13~15位为顺序号,并能够判断性别,奇数为男,偶数为女。
    regex: [[:digit:]]{8}(1[0-2]|0[1-9])(0[1-9]|[1-3][0-9])[[:digit:]]{3}
    
    
    18位的身份证号码 如:
    1~6位为地区代码
    7~14位为出生年月日
    15~17位为顺序号
    18位为校验位包括x
    regex: [[:digit:]]{6}(19[[:digit:]]{2}|20[[:digit:]]{2})(1[0-2]|0[1-9])(0[1-9]|[1-3][0-9])[[:digit:]]{2}[[:digit:]xX]
    
    最后的regex:([[:digit:]]{8}(1[0-2]|0[1-9])(0[1-9]|[1-3][0-9])[[:digit:]]{3}|[[:digit:]]{6}(19[[:digit:]]{2}|20[[:digit:]]{2})(1[0-2]|0[1-9])(0[1-9]|[1-3][0-9])[[:digit:]]{2}[[:digit:]xX]) 
    
    11、用正则表达式表示手机号
    regex: [[:digit:]]{11}
    12、用正则表达式表示邮箱: x@y.z.m
    regex:[[:alpha:]][[:alnum:]_.]{0,20}@[[:alnum:]_]{1,30}(.[[:alnum:]_]{1,30})*
    Vim的正则表达式

    1、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符

    cp  /etc/profile /tmp/profile

    vim /tmp/profile

    :%s@^[[:space:]]*@@g

    2、复制/etc/rc.d/init.d/functions文件至/tmp目录,用查找替换命令为/tmp/functions的每行开头为空白字符的行的行首添加一个#号

    cp /etc/rc.d/init.d/functions /tmp/functions

    vim /tmp/functions

    :%s@^[[:space:]]@#&@g

  • 相关阅读:
    jsp和servlet的联系与区别
    tomcat会如何处理请求
    java中synchronized的关键字
    mybatis的动态sql
    spring自带的json转换工具,支持@ResponseBody注解
    layer一个web弹窗/层解决方案
    spring mvc出现乱码的问题
    hdu1010 Tempter of the Bone
    hdu 3342 Legal or Not
    ThreadPoolExecutor线程池执行器源码解析
  • 原文地址:https://www.cnblogs.com/momenglin/p/8483094.html
Copyright © 2020-2023  润新知