【shell】sed:对文本查找后进行:替换、删除、插入、更改文本

发布时间:2026/11/3 9:10:34

【shell】sed:对文本查找后进行:替换、删除、插入、更改文本 文章目录一. 基本认识二. 常见用法1. 多编辑e2. 删除d3. 查找替换s4. 字符转换y5. 插入文本i、a6. 按需打印p三. sed脚本四. 使用变量进行匹配替换一. 基本认识sed处理文本的基本逻辑sed处理文本时是以行为单位的处理时把当前处理的行存储在临时缓冲区中接着用sed 命令处理缓冲区中的内容处理完成后把缓冲区的内容送往屏幕。然后再处理下一行直至全文处理结束。sed可以删除、查找替换、添加、插入、从其他文件中读入数据等。常用场景是太过于庞大的文本使用常规编辑器难以胜任比如说vi一个几百兆的文件。有规律的文本修改加快文本处理速度比如说全文替换。常用选项-n不输出模式空间内容到屏幕即不自动打印只打印匹配到的行-e多点编辑对每行处理时可以有多个Script-f把Script写到文件当中在执行sed时-f 指定文件路径如果是多个Script换行写-r支持扩展的正则表达式-i直接将处理的结果写入文件-i.bak在将处理的结果写入文件之前备份一份二. 常见用法1. 多编辑e使用-e参数或分号连接多编辑命令#两个编辑命令前都要使用-e参数如果有更多的编辑需求以此类推即可[rootlocalhost~]# sed-es/this/That/g-es/line/LINE/gSed.txt That is LINE1,That is First LINE That is LINE2,the Second LINE,Empty LINE followed That is LINE4,That is Third LINE That is LINE5,That is Fifth LINE #上面的命令可以用分号改写为[rootlocalhost~]# seds/this/That/g ; s/line/LINE/gSed.txt2. 删除d使用d命令可删除指定的行(源文件没有更改)示例如下#将file的第一行删除后输出到屏幕[rootlocalhost~]# sed1dSed.txt this is line2,the Second line,Empty line followed this is line4,this is Third line this is line5,this is Fifth line范围删除#删除指定范围的行第1行到第3行[rootlocalhost~]# sed1,3dSed.txt this is line4,this is Third line this is line5,this is Fifth line #删除指定范围的行第一行到最后行[rootlocalhost~]# sed1,$dSed.txt[rootlocalhost~]# #清空了Sed.txt文件其他特殊位置删除#删除最后一行[rootlocalhost~]# sed$dSed.txt this is line1,this is First line this is line2,the Second line,Empty line followed this is line4,this is Third line #删除除指定范围以外的行只保留第5行 #要删除其他的行请举一反三[rootlocalhost~]# sed5!dSed.txt this is line5,this is Fifth line #删除所有包含Empty的行[rootlocalhost~]# sed/Empty/dSed.txt this is line1,this is First line this is line4,this is Third line this is line5,this is Fifth line #删除空行[rootlocalhost~]# sed/^$/dSed.txt this is line1,this is First line this is line2,the Second line,Empty line followed this is line4,this is Third line this is line5,this is Fifth line3. 查找替换s使用s可将查找到的文本替换为新的文本。替换开头#只替换开头的this为that[rootlocalhost~]# seds/^this/that/Sed.txt that is line1,this is First line that is line2,the Second line,Empty line followed that is line4,this is Third line that is line5,this is Fifth line替换行内前几个#s命令用于替换文本本例中使用LINE替换line#请注意每一行只有第一个line被替换了默认情况下只替换第一次匹配到的内容[rootlocalhost~]# seds/line/LINE/Sed.txt this is LINE1,this is First line this is LINE2,the Second line,Empty line followed this is LINE4,this is Third line this is LINE5,this is Fifth line #要想每行最多匹配2个line并改为LINE可用如下方式 #注意到第2行中有3个line前两个被替换了第三个没有变化[rootlocalhost~]# seds/line/LINE/2Sed.txt this is line1,this is First LINE this is line2,the Second LINE,Empty line followed this is line4,this is Third LINE this is line5,this is Fifth LINE替换所有匹配#s命令利用g选项可以完成所有匹配值的替换[rootlocalhost~]# seds/line/LINE/gSed.txt this is LINE1,this is First LINE this is LINE2,the Second LINE,Empty LINE followed this is LINE4,this is Third LINE this is LINE5,this is Fifth LINE4. 字符转换yy的逻辑是将一系列字符逐个地变换为另外一系列字符基本用法如下#该命令会将file中的O转换为N、L转换为E、D转换为W #注意转换字符和被转换字符的长度要相等否则sed无法执行 sedy/OLD/NEW/file[rootlocalhost~]# sedy/1245/ABCD/Sed.txt this is line A,this is First line this is line B,the Second line,Empty line followed this is line C,this is Third line this is line D,this is Fifth line5. 插入文本i、a要想保存修改后的文件必须使用重定向生成新的文件。如果想直接修改源文件本身则需要使用“-i”参数。使用i或a命令插入文本其中i代表在匹配行之前插入而a代表在匹配行之后插入。指定行插入文本#使用i在第二行前插入文本[rootlocalhost~]# sed2 i InsertSed.txt this is line1,this is First line Insert this is line2,the Second line,Empty line followed this is line4,this is Third line this is line5,this is Fifth line #使用a在第二行后插入文本[rootlocalhost~]# sed2 a InsertSed.txt this is line1,this is First line this is line2,the Second line,Empty line followed Insert this is line4,this is Third line this is line5,this is Fifth line匹配行插入#在匹配行的上一行插入问题[rootlocalhost~]# sed/Second/i\InsertSed.txt this is line1,this is First line Insert this is line2,the Second line,Empty line followed this is line4,this is Third line this is line5,this is Fifth line6. 按需打印p使用p命令可进行打印这里使用sed命令时一定要加-n参数表示不打印没关系的行。由于sed的工作原理是基于行的因此每次都有大量的输出。可是这些输出中有一些是我们并不需要看到的而只需要输出匹配的行或者处理过的行就好了。sed的问题#将the替换成THE#sed实际处理了第二行其他几行由于没有匹配所以并未真正处理#但是sed的工作原理是基于流的所以所有流过的行都打印出来了[rootlocalhost~]# seds/the/THE/Sed.txt this is line1,this is First line this is line2,THE Second line,Empty line followed this is line4,this is Third line this is line5,this is Fifth line打印指定的行#打印出文件中指定的行[rootlocalhost~]# sed-n1pSed.txt this is line1,this is First line #使用p命令则只打印实际处理过的行简化了输出使用-n参数[rootlocalhost~]# sed-ns/the/THE/pSed.txt this is line2,THE Second line,Empty line followed三. sed脚本定期对一些文件做分析操作这种例行的工作往往有一定“标准化”的操作比如说先去除文件中所有的空行然后再全部替换某些字符等这种过程类似于生产线上程式化的流水作业。#该sed脚本的作用是将全文的this改为THAT并删除所有空行[rootlocalhost~]# cat Sed.rules s/this/THAT/g/^$/d[rootlocalhost~]# sed-f Sed.rules Sed.txt THAT is line1,THAT is First line THAT is line2,the Second line,Empty line followed THAT is line4,THAT is Third line THAT is line5,THAT is Fifth line四. 使用变量进行匹配替换export newgood echohello old frank|seds/old/${new}/g
延伸阅读

更多相关文章

2026/11/3 9:10:34

【netty基础四】netty与nio

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/3 9:10:34

hive CUSTOM authentication mode:通过用户名密码连接hiveserver

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/3 9:10:34

【shell】eval命令用法:简化shell结构 封装多个命令然后执行

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/3 10:16:57

5分钟跑通 Intern-S2-397B:LMDeploy/vLLM/SGLang 三选一部署手册

5分钟跑通 Intern-S2-397B:LMDeploy/vLLM/SGLang 三选一部署手册 上海AI实验室开源的 Intern-S2-397B 把"科学智能体"从口号变成了可以本地自托管的现实:397B 参数的 MoE 多模态模型、原生 256K 上下文、Apache-2.0 许可,官方同时…

2026/11/3 10:16:57

Nex-N2.5-mini 边缘端部署:SGLang 智能体推理 + 函数调用最新姿势

Nex-N2.5-mini 边缘端部署:SGLang 智能体推理 函数调用最新姿势 在开源智能体模型这条赛道上,"能想、会动手"正在成为新的及格线:模型不仅要会生成文本,还要能在多轮工具调用中自主规划、执行并依据反馈自纠错。Nex-N…

2026/11/3 10:11:57

【Agent-01】理解 Agent:目标、执行与证据

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/3 7:47:06

面向复杂光照与角度的宠物面部识别鲁棒性增强策略

在智慧养宠时代,基于视觉的宠物身份识别技术正逐步从实验室走向家庭应用。然而,与配合度高的人类不同,宠物是典型的“非合作生物识别对象”——它们不会主动配合摄像头调整姿态,更无法控制光照环境。实际应用中,我们常…

2026/11/2 6:39:12

SSM电子书店源码全解析:结构、原理、部署与排错实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/3 1:04:48

STM32实战:基于MAX14431数字隔离器的SPI隔离通信方案

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/3 1:04:48

工业SPI隔离通信设计:MAX14431BAEE+与STM32L4A6RG实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/3 1:04:48

STM32+NRF24L01无线抢答器:硬件设计、通信协议与低功耗实现

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/11/3 7:47:06

面向复杂光照与角度的宠物面部识别鲁棒性增强策略

在智慧养宠时代,基于视觉的宠物身份识别技术正逐步从实验室走向家庭应用。然而,与配合度高的人类不同,宠物是典型的“非合作生物识别对象”——它们不会主动配合摄像头调整姿态,更无法控制光照环境。实际应用中,我们常…

2026/11/2 6:39:12

SSM电子书店源码全解析:结构、原理、部署与排错实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑