• .Linux基础正则表达式字符


    1. ^oldboy 以oldboy开头
    2. oldboy$ 以oldboy结尾
    3. ^$ 空行
    4. . 匹配任意单个字符
    5. * 重复前一个字符0或n次
    6. .* 匹配所有
    c. 数据准备
    1. #重要说明:Linux基础正则表达式仅适用于grep、sed、awk、egrep(grep -E)
    2. [root@oldboyedu ~]# touch file{01..05}.txt
    3. [root@oldboyedu ~]# touch {01..05}.txt
    4. [root@oldboyedu ~]# ls
    5. 01.txt 03.txt 05.txt file02.txt file04.txt
    6. 02.txt 04.txt file01.txt file03.txt file05.txt
    d. 过滤出以 file开头的所有文件
    1. ##方法1:利用通配符*匹配
    2. [root@oldboyedu ~]# ls file*
    3. file01.txt file02.txt file03.txt file04.txt file05.txt
    4. ##方法2:利用正则^
    5. [root@oldboyedu ~]# ls |grep "^file"
    6. file01.txt
    7. file02.txt
    8. file03.txt
    9. file04.txt
    10. file05.txt

    e. 过滤出以 txt 结尾的所有文件
    1. [root@oldboyedu ~]# ls|grep "txt$"
    2. 01.txt
    3. 02.txt
    4. 03.txt
    5. 04.txt
    6. 05.txt
    7. file01.txt
    8. file02.txt
    9. file03.txt
    10. file04.txt
    11. file05.txt

    f. 过滤空行( ^$
    1. ##测试准备
    2. [root@oldboyedu ~]# echo 1 >>file01.txt
    3. [root@oldboyedu ~]# echo >>file01.txt ##生成空行
    4. grep的-E选项,允许同时过滤多组内容,等价于egrep。
    5. h.过滤含有0后是任意字符的文件
    6. [root@oldboyedu ~]# echo >>file01.txt ##生成空行
    7. [root@oldboyedu ~]# echo 2 >>file01.txt
    8. [root@oldboyedu ~]# cat file01.txt
    9. 1
    10. 2
    11. ## 过滤空行
    12. [root@oldboyedu ~]# grep "^$" file01.txt
    13. ## 排除空行
    14. [root@oldboyedu ~]# grep -v "^$" file01.txt
    15. 1
    16. 2

    g. 同时过滤出含有 file 开头和 jpg 结尾的所有文件
    1. ##准备测试数据
    2. [root@oldboyedu ~]# touch a{1..3}.jpg
    3. [root@oldboyedu ~]# ls
    4. 01.txt 03.txt 05.txt a2.jpg file01.txt file03.txt file05.txt
    5. 02.txt 04.txt a1.jpg a3.jpg file02.txt file04.txt
    6. ##同时过滤出含有file开头以及jpg结尾的所有文件
    7. [root@oldboyedu ~]# ls|grep -E "^file|jpg$"
    8. a1.jpg
    9. a2.jpg
    10. a3.jpg
    11. file01.txt
    12. file02.txt
    13. file03.txt
    14. file04.txt
    15. file05.txt
    16. [root@oldboyedu ~]# ls|egrep "^file|jpg$"
    17. a1.jpg
    18. a2.jpg
    19. a3.jpg
    20. file01.txt
    21. file02.txt
    22. file03.txt
    23. file04.txt
    24. file05.txt
    ## 实验:
    yum install nginx -y
    egrep -v "^ $| #" /etc/nginx/nginx.conf [root@oldboyedu ~] # ls|grep "0."
    01 .txt
    02 .txt
    03 .txt
    04 .txt
    05 .txt
    file01.txt
    file02.txt
    file03.txt
    file04.txt
    file05.txt
    i. 过滤以 0 及后是任意字符【开头】的文件
    [root@oldboyedu ~] # ls|grep "^0."
    01 .txt
    02 .txt
    03 .txt
    04 .txt
    05 .txt
    j. 过滤包含 file, 并且后面有 0 0 ,以及多个 0 的文件。
    [root@oldboyedu ~] # touch file00
    [root@oldboyedu ~] # touch file000
    [root@oldboyedu ~] # ls|grep "file0*"
    file01.txt
    file02.txt
    file03.txt
    file04.txt
    file05.txt
    file00
    file000
    k. 过滤包含 file0 ,然后后面任意多个字符的文件
    [root@oldboyedu ~] # touch file0x
    [root@oldboyedu ~] # touch file0y
    [root@oldboyedu ~] # touch file0yyy
    [root@oldboyedu ~] # ls|grep "file0.*" ##.* 表示所有
    file00
    file000
    file01.txt
    file02.txt
    file03.txt
    file04.txt
    file05.txt
    file0x
    file0y
    file0yyy
    ## 学习:根据结果反推命令的意义。
  • 相关阅读:
    c++ vs2019 cpp20 规范,set源码分析
    【编译器】2022-11-VSCode配置编译与调试C++程序(含输入输出重定向)
    分享的ise文件synthesize出错,如何解决?
    take和 drop功能还有takewhile 和 dropwhile 功能主要用于分开list
    【数据结构】数组和字符串(九):稀疏矩阵的链接存储:十字链表的插入、查找、删除操作
    Android GB28181设备接入端语音广播和语音对讲技术实现探究
    基于R-Tree的地理空间数据分析加速
    基于SpringBoot的商品物品产品众筹平台设计与实现(源码+lw+部署文档+讲解等)
    【机器学习】特征工程:特征预处理,归一化、标准化、处理缺失值
    Ubuntu 22.04 中安装 fcitx5
  • 原文地址:https://blog.csdn.net/weixin_56233402/article/details/137440134