• 扩展正则表达式


    扩展正则表达式
    1.显示三个用户root,cisco,ccnp的UID和默认shell (普通正则表达式与扩展正则表达式)
    [17:27:08 root@localhost data]#cat /etc/passwd |grep '^root|^ccnp|^cisco' |cut -d: -f1,3,7
    root:0:/bin/bash
    cisco:1000:/bin/bash
    ccnp:1006:/bin/bash 

    [17:16:48 root@localhost data]#cat /etc/passwd |grep -E '^(root|ccnp|cisco)' |cut -d: -f1,3,7
    root:0:/bin/bash
    cisco:1000:/bin/bash
    ccnp:1006:/bin/bash

    [20:45:33 root@localhost ~]#cat /etc/passwd |grep -Ew '^(root|ccnp|cisco)' |cut -d: -f1,3,7

    root:0:/bin/bash
    cisco:1000:/bin/bash
    ccnp:1006:/bin/bash

    2.找出/etc/rc.d/init.d/functions文件中行首为某单词(保护下划线)后面跟一个小括号的行
    cat /etc/rc.d/init.d/functions |grep -E -o '^([[:alnum:]]|_)+()*'

    3.使用egrep 取出/etc/rc.d/init.d/funcitons
    [19:24:44 root@localhost data]#echo /etc/rc.d/init.d/functions |grep -Eo "[^/]+$"
    functions

    4.使用egrep取出上面路径的目录名
    [19:27:51 root@localhost data]#echo /etc/rc.d/init.d/functions |grep -Eo '^/.*/'
    /etc/rc.d/init.d/

    5.利用扩展正则表达式分别表示0-9,10-99,100-100,200-249,250.-255
    echo 1 10 101 201 250 252 > f1
    egrep "[[:digit:]]{1}>" f1
    egrep "[1-9][[:digit:]]{1}>" f1
    egrep "1[[:digit:]]{2}>" f1
    egrep "2[0-4][[:digit:]]{1}>" f1
    egrep "25[0-5]" f1

    6.显示ifconfig命令结果中所有ipv4地址
    ifconfig |grep -o 'inet *.*.*.*' |cut -d' ' -f2
    ifconfig |grep 'inet' | tr -s ' ' |cut -d ' ' -f3

    ifconfig |grep -w inet | tr -s ' ' |cut -d ' ' -f3

    7.将此字符串:中的每个字符去重并排序,重复次数多的排到前面
    echo 'welcom tooo linuxx' | grep -o [[:alpha:]]|uniq -c | sort -n
    cat /etc/motd | grep -o [[:alpha:]]|uniq -c | sort -nr

    8.取网卡第一个IP地址

    ifconfig |grep -Eo '([0-9]{1,3}.){3}[0-9]{1,3}'|head -1

  • 相关阅读:
    C# 轻松读取、改变文件的创建、修改、访问时间 z
    C#中将dll汇入exe z
    ASP.NET中引用dll“找不到指定模块"的完美解决办法 z
    C# 调用第三方DLL z
    [ACM_贪心] Radar Installation
    Beauty Contest
    [ACM_几何] Wall
    [ACM_几何] Pipe
    [ACM_几何] Fishnet
    [ACM_动态规划] 找零种类
  • 原文地址:https://www.cnblogs.com/HRAS/p/12826558.html
Copyright © 2020-2023  润新知