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

目录

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

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

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


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

Sys=1
    ManageState=ON
    Configuration=1
        TcpGroup=1
            cpuThreshold=70
            bwThreshold=80
            TcpChannel=1
               localPort=1110
               remoteIp=10.0.6.1
               remotePort=1111
            TcpChannel=2
               localPort=2221
               remoteIp=10.0.6.2
               remotePort=2222
            TcpChannel=3
               localPort=3332
               remoteIp=10.0.6.3
               remotePort=3333
            TcpChannel=4
               localPort=4443
               remoteIp=10.0.6.4
               remotePort=4444
            TcpChannel=5
               localPort=5554
               remoteIp=10.0.6.5
               remotePort=5555
        LogLevel=ALL

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

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

[root@xxx ts]# grep 'TcpChannel' confile.txt
            TcpChannel=1
            TcpChannel=2
            TcpChannel=3
            TcpChannel=4
            TcpChannel=5
[root@xxx ts]#
[root@xxx ts]# grep -n 'TcpChannel' confile.txt
7:            TcpChannel=1
11:            TcpChannel=2
15:            TcpChannel=3
19:            TcpChannel=4
23:            TcpChannel=5
[root@xxx ts]#

查找TcpChannel及其后一行 -A1

[root@xxx ts]# grep -A1 'TcpChannel' confile.txt
            TcpChannel=1
               localPort=1110
--
            TcpChannel=2
               localPort=2221
--
            TcpChannel=3
               localPort=3332
--
            TcpChannel=4
               localPort=4443
--
            TcpChannel=5
               localPort=5554
[root@xxx ts]#

 查找remotePort及其前一行 -B1

[root@xxx ts]# grep -B1 'remotePort' confile.txt
               remoteIp=10.0.6.1
               remotePort=1111
--
               remoteIp=10.0.6.2
               remotePort=2222
--
               remoteIp=10.0.6.3
               remotePort=3333
--
               remoteIp=10.0.6.4
               remotePort=4444
--
               remoteIp=10.0.6.5
               remotePort=5555
[root@xxx ts]#

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

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

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

[root@xxx ts]# sed '/TcpChannel=2/,+3d' confile.txt
Sys=1
    ManageState=ON
    Configuration=1
        TcpGroup=1
            cpuThreshold=70
            bwThreshold=80
            TcpChannel=1
               localPort=1110
               remoteIp=10.0.6.1
               remotePort=1111
            TcpChannel=3
               localPort=3332
               remoteIp=10.0.6.3
               remotePort=3333
            TcpChannel=4
               localPort=4443
               remoteIp=10.0.6.4
               remotePort=4444
            TcpChannel=5
               localPort=5554
               remoteIp=10.0.6.5
               remotePort=5555
        LogLevel=ALL
[root@xxx ts]#

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

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

[root@xxx ts]# sed  -i  "/LogLevel/i\            TcpChannel=6" confile.txt
[root@xxx ts]# sed  -i  "/LogLevel/i\               localPort=6665" confile.txt
[root@xxx ts]# sed  -i  "/LogLevel/i\               remoteIp=10.0.6.6" confile.txt
[root@xxx ts]# sed  -i  "/LogLevel/i\               remotePort=6666" confile.txt
[root@xxx ts]#
[root@xxx ts]#
[root@xxx ts]# cat confile.txt
Sys=1
    ManageState=ON
    Configuration=1
        TcpGroup=1
            cpuThreshold=70
            bwThreshold=80
            TcpChannel=1
               localPort=1110
               remoteIp=10.0.6.1
               remotePort=1111
            TcpChannel=2
               localPort=2221
               remoteIp=10.0.6.2
               remotePort=2222
            TcpChannel=3
               localPort=3332
               remoteIp=10.0.6.3
               remotePort=3333
            TcpChannel=4
               localPort=4443
               remoteIp=10.0.6.4
               remotePort=4444
            TcpChannel=5
               localPort=5554
               remoteIp=10.0.6.5
               remotePort=5555
            TcpChannel=6
               localPort=6665
               remoteIp=10.0.6.6
               remotePort=6666
        LogLevel=ALL
[root@xxx ts]#

说明:

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

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

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

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

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值