• [Linux]shell文本处理记录 - 查找、增删特定行及附近行


    目录

    1.grep查找关键字所在行号、查找关键字前后行

    2.sed删除指定行及其前后若干行

    3.sed在匹配行前或后添加一行,并且保留空格


    考虑如下测试配置文件 confile.txt

    1. Sys=1
    2. ManageState=ON
    3. Configuration=1
    4. TcpGroup=1
    5. cpuThreshold=70
    6. bwThreshold=80
    7. TcpChannel=1
    8. localPort=1110
    9. remoteIp=10.0.6.1
    10. remotePort=1111
    11. TcpChannel=2
    12. localPort=2221
    13. remoteIp=10.0.6.2
    14. remotePort=2222
    15. TcpChannel=3
    16. localPort=3332
    17. remoteIp=10.0.6.3
    18. remotePort=3333
    19. TcpChannel=4
    20. localPort=4443
    21. remoteIp=10.0.6.4
    22. remotePort=4444
    23. TcpChannel=5
    24. localPort=5554
    25. remoteIp=10.0.6.5
    26. remotePort=5555
    27. LogLevel=ALL

    1.grep查找关键字所在行号、查找关键字前后行

    查找含有关键字的行,带 -n显示行号

    1. [root@xxx ts]# grep 'TcpChannel' confile.txt
    2. TcpChannel=1
    3. TcpChannel=2
    4. TcpChannel=3
    5. TcpChannel=4
    6. TcpChannel=5
    7. [root@xxx ts]#
    8. [root@xxx ts]# grep -n 'TcpChannel' confile.txt
    9. 7: TcpChannel=1
    10. 11: TcpChannel=2
    11. 15: TcpChannel=3
    12. 19: TcpChannel=4
    13. 23: TcpChannel=5
    14. [root@xxx ts]#

    查找TcpChannel及其后一行 -A1

    1. [root@xxx ts]# grep -A1 'TcpChannel' confile.txt
    2. TcpChannel=1
    3. localPort=1110
    4. --
    5. TcpChannel=2
    6. localPort=2221
    7. --
    8. TcpChannel=3
    9. localPort=3332
    10. --
    11. TcpChannel=4
    12. localPort=4443
    13. --
    14. TcpChannel=5
    15. localPort=5554
    16. [root@xxx ts]#

     查找remotePort及其前一行 -B1

    1. [root@xxx ts]# grep -B1 'remotePort' confile.txt
    2. remoteIp=10.0.6.1
    3. remotePort=1111
    4. --
    5. remoteIp=10.0.6.2
    6. remotePort=2222
    7. --
    8. remoteIp=10.0.6.3
    9. remotePort=3333
    10. --
    11. remoteIp=10.0.6.4
    12. remotePort=4444
    13. --
    14. remoteIp=10.0.6.5
    15. remotePort=5555
    16. [root@xxx ts]#

    2.sed删除指定行及其前后若干行

    删除Channel 2的相关信息 --> 删除Channel2及其后3行(-i写入文件)

    sed '/TcpChannel=2/,+3d' confile.txt

    1. [root@xxx ts]# sed '/TcpChannel=2/,+3d' confile.txt
    2. Sys=1
    3. ManageState=ON
    4. Configuration=1
    5. TcpGroup=1
    6. cpuThreshold=70
    7. bwThreshold=80
    8. TcpChannel=1
    9. localPort=1110
    10. remoteIp=10.0.6.1
    11. remotePort=1111
    12. TcpChannel=3
    13. localPort=3332
    14. remoteIp=10.0.6.3
    15. remotePort=3333
    16. TcpChannel=4
    17. localPort=4443
    18. remoteIp=10.0.6.4
    19. remotePort=4444
    20. TcpChannel=5
    21. localPort=5554
    22. remoteIp=10.0.6.5
    23. remotePort=5555
    24. LogLevel=ALL
    25. [root@xxx ts]#

    3.sed在匹配行前或后添加一行,并且保留空格

    新增Channel 6 --> 例如在LogLevel行前依次插入数据

    1. [root@xxx ts]# sed -i "/LogLevel/i\ TcpChannel=6" confile.txt
    2. [root@xxx ts]# sed -i "/LogLevel/i\ localPort=6665" confile.txt
    3. [root@xxx ts]# sed -i "/LogLevel/i\ remoteIp=10.0.6.6" confile.txt
    4. [root@xxx ts]# sed -i "/LogLevel/i\ remotePort=6666" confile.txt
    5. [root@xxx ts]#
    6. [root@xxx ts]#
    7. [root@xxx ts]# cat confile.txt
    8. Sys=1
    9. ManageState=ON
    10. Configuration=1
    11. TcpGroup=1
    12. cpuThreshold=70
    13. bwThreshold=80
    14. TcpChannel=1
    15. localPort=1110
    16. remoteIp=10.0.6.1
    17. remotePort=1111
    18. TcpChannel=2
    19. localPort=2221
    20. remoteIp=10.0.6.2
    21. remotePort=2222
    22. TcpChannel=3
    23. localPort=3332
    24. remoteIp=10.0.6.3
    25. remotePort=3333
    26. TcpChannel=4
    27. localPort=4443
    28. remoteIp=10.0.6.4
    29. remotePort=4444
    30. TcpChannel=5
    31. localPort=5554
    32. remoteIp=10.0.6.5
    33. remotePort=5555
    34. TcpChannel=6
    35. localPort=6665
    36. remoteIp=10.0.6.6
    37. remotePort=6666
    38. LogLevel=ALL
    39. [root@xxx ts]#

    说明:

    sed  -i  "/要匹配的/i\  新的内容 "  filename

    sed  -i  "/要匹配的/a\  新的内容 "  filename

    -i: 是在文件内部操作
    第一个/和第二个/:固定写法
    双引号内部的i:在前面插入 ;双引号内部的a:在匹配行后面添加
    反斜杠\ 后的内容都作为输出,包括空格 

    参考资料:sed在匹配行前或后添加一行,并且保留空格 https://blog.csdn.net/qq_39677803/article/details/122626572

  • 相关阅读:
    必备的团队任务协同看板工具及共享思维导图软件
    CommomJS使用介绍_web前端培训
    WZOI-260近在咫尺
    C#winfrom端屏幕截图功能的简单实现(修改了屏幕的缩放比例后,截图功能异常,慎用!!!)
    MongoDB 简介、特点、安装、核心概念
    荧光染料AF488 DBCO, 5-isomer,AF488 二苯基环辛炔, 5-异构体
    使用easyexcel将图片批量写入excel
    通过pip,查看tensorflow和tensorflow-probaility版本
    nuitka傻瓜式打包命令
    银行自动化运维项目前期规划八大难点
  • 原文地址:https://blog.csdn.net/wy_hhxx/article/details/127416595