首页 > 系统相关 >[Linux] shell文本处理记录 - 查找、增删特定行及附近行

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

时间:2023-09-28 17:55:53浏览次数:32  
标签:remotePort shell localPort 10.0 TcpChannel 行及 ts 文本处理 remoteIp

转:https://blog.csdn.net/wy_hhxx/article/details/127416595

查找username所在行并删除此行,输出到新文件
sed '/username/,+d' 04filename.log > 04filename_new.log

 

目录

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

翻译

搜索

复制

标签:remotePort,shell,localPort,10.0,TcpChannel,行及,ts,文本处理,remoteIp
From: https://www.cnblogs.com/qsds/p/17736266.html

相关文章

  • Powershell 获取AD Certificate 详细信息
    get-aduser-SearchBase$ou-Filter*-Propertiesdisplayname,usercertificate|ForEach-Object{$displayname=$_.displayname$_|select-ExpandPropertyusercertificate|ForEach-Object{$cert=[System.Security.Cryptography.X509Certifi......
  • shell遍历比较文件夹下文件md5值
    #!/bin/bashCURRENT_DIR=$(cd$(dirname$0);pwd)SOURCE_DIR="$CURRENT_DIR/python_data"TARGET_DIR="$CURRENT_DIR/out_bin"cd$SOURCE_DIR>python.md5forfilein$(ls$SOURCE_DIR|grep"data")dosource_file=${SOURCE_......
  • 掌握Shell用户管理,让你的系统运行更顺畅!
    用户帐号帐号操作主要是增、删、改、禁。Linux系统提供了底层的 useradd, userdel 和 usermod 来完成相关操作,也提供了进一步的简化封装:adduser, deluser。为了避免混淆,咱们这里只介绍最底层的指令,这些指令设计上已经够简洁明了方便。由于只有系统管理员才能创建新用户,请确......
  • How to add a string that contains whitespace to array in shell script All In One
    HowtoaddastringthatcontainswhitespacetoarrayinshellscriptAllInOneIhavetriedsomewaystoaddastringwhichcontainwhitespacetoarrayinshellscript,butfailed.stringvariablesconcatenate#!/usr/bin/envbashstr1="hello&qu......
  • linux-Shell将命令行终端输出结果写入保存到文件中
    (一)将输出与错误写到同一个文件(1)方法1#!bin/bashjava-jarhbase-example.jar2>&1|teehbase_log.txt说明:0,1,2:在linux分别表示标准输入、标准输出和标准错误信息输出。tee默认为写入覆盖,-a参数表示追加内容。#!bin/bashjava-jarhbase-example.jar2>&1|tee-ahbase_......
  • Linux shell编程学习笔记1:关于shell的前世今生
    一、什么是Shell?Shell英文单词的原意是“外壳”,在计算机领域专指在操作系统(OperatingSystem)外层,提供用户界面(UserInterface)的程序,主要负责将用户的命令(Command)转化为操作系统可识别的指令(Instruction)。二、UnixshellUnix诞生于1969年,是最早提供shell,从而将操作系统和用户界面......
  • pycharm无法打开终端:open Local Terminal_Failed to start [powershell.exe]
    今天在运行pycharm的时候出现了这个问题openLocalTerminal_Failedtostart[powershell.exe]直接上解决办法1.进入设置2.选择tools下的terminal然后修改shellpath 如果没有的话需要找到本机的powershell的路径然后对其进行修改就能正常运行了  ......
  • Linux shell script if condition control flow methods All In One
    LinuxshellscriptifconditioncontrolflowmethodsAllInOneif...then...fi/if...then...else..fi/if...then...elif...then...fi#!/usr/bin/envbashifbugthenecho"bug✅"elseecho"bug❌"fiifpwdthenecho"pwd......
  • SHELL简介
    1.简介2.基本元素2.1命令与参数$cdword;ls-lwhizprog.c-rw-r--r-- 1 tolstoy devel 30252 Jul 922:52whizprog.c$make...空白分割命令行中各个组成部分;命令名称是命令行第一个项目,后面跟着选项;选项开头使用-,不带参数的选项可以合并,如-l-t可以写为-lt;分号;......
  • shell脚本中的EOF是什么
    概述在Shell脚本中,EOF(EndofFile)是一个特殊标记,用于指示一段文本的开始和结束位置。但它并不是Shell脚本中的关键字或保留字。您可以自由选择EOF之前的标记,只需确保开始和结束标记匹配即可。基本上<<EOF告诉shell您将输入多行字符串,直到“标记”EOF。您可以根据需要命名此标......