• .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
    ## 学习:根据结果反推命令的意义。
  • 相关阅读:
    DSP 开发例程: led_flash
    【Jackson】自定义注解结合Jackson
    【NodeJs-5天学习】第三天实战篇④ ——QQ机器人,实现自动回复、重要提醒
    【下一代对象专题】小文件管理全线升级,性能提升60%
    3.1 栈和队列的定义和特点
    【电源专题】什么是开关稳压器
    【深度学习框架格式转化】【GPU】Pytorch模型转ONNX模型格式流程详解【入门】
    springboot基于java的康泰小区物业管理系统的设计与实现毕业设计源码101926
    【LeetCode刷题-双指针】--16.最接近的三数之和
    【华为机试真题Java】用户调度问题
  • 原文地址:https://blog.csdn.net/weixin_56233402/article/details/137440134