前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >sed命令工作原理及命令备忘

sed命令工作原理及命令备忘

作者头像
jeremyxu
发布于 2018-05-10 03:16:35
发布于 2018-05-10 03:16:35
1K00
代码可运行
举报
运行总次数:0
代码可运行

sed是一个非交互式的流编辑器(stream editor)。所谓非交互式,是指使用sed只能在命令行下输入编辑命令来编辑文本,然后在屏幕上查看输出;而所谓流编辑器,是指sed每次只从文件(或输入)读入一行,然后对该行进行指定的处理,并将结果输出到屏幕(除非取消了屏幕输出又没有显式地使用打印命令),接着读入下一行。整个文件像流水一样被逐行处理然后逐行输出。

工作中经常会使用sed命令对文件进行各种操作,之前一直对它的工作原理不是很了解,只不过在网上抄一些命令完成操作,有时遇到了问题,就问一问身边的“脚本小王子”,基本上都可以搞定。今天下班了决定对sed命令深入学习一下。

工作原理

核心逻辑

sed一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区(pattern space)中的内容,处理完成后,把缓冲区(pattern space)的内容送往屏幕。接着清空缓冲区(pattern space),处理下一行,这样不断重复,直到文件末尾。

sed里有两个空间:pattern space与hold space。

pattern space(模式空间)相当于车间sed把流内容在这里处理;

hold space(保留空间)相当于仓库,加工的半成品在这里临时储存(当然加工完的成品也在这里存储)。

sed处理每一行的逻辑:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1. 先读入一行,去掉尾部换行符,存入pattern space,执行编辑命令。

2. 处理完毕,除非加了-n参数,把现在的pattern space打印出来,在后边打印曾去掉的换行符。

3. 把pattern space内容给hold space,把pattern space置空。

4. 接着读下一行,处理下一行。

命令组织形式

sed最重要的命令组织形式可以概括为*** address[,address]{cmd} ***

address

address可以是一个数字,也可以是一个模式,你可以通过逗号要分隔两个address 表示两个address的区间

man手册上对address的理解比较全面

1234567891011121314151617181920212223242526272829303132333435

Addresses Sed commands can be given with no addresses, in which case the command will be executed for all input lines; with one address, in which case the command will only be executed for input lines which match that address; or with two addresses, in which case the command will be executed for all input lines which match the inclusive range of lines starting from the first address and continuing to the second address. Three things to note about address ranges: the syntax is addr1,addr2 (i.e., the addresses are separated by a comma); the line which addr1 matched will always be accepted, even if addr2 selects an earlier line; and if addr2 is a regexp, it will not be tested against the line that addr1 matched. After the address (or address-range), and before the command, a ! may be inserted, which specifies that the command shall only be executed if the address (or address-range) does not match. The following address types are supported: number Match only the specified line number (which increments cumulatively across files, unless the -s option is specified on the command line). first~step Match every step'th line starting with line first. For example, ``sed -n 1~2p'' will print all the odd-numbered lines in the input stream, and the address 2~5 will match every fifth line, starting with the second. first can be zero; in this case, sed operates as if it were equal to step. (This is an extension.) $ Match the last line. /regexp/ Match lines matching the regular expression regexp. \cregexpc Match lines matching the regular expression regexp. The c may be any character. GNU sed also supports some special 2-address forms: 0,addr2 Start out in "matched first address" state, until addr2 is found. This is similar to 1,addr2, except that if addr2 matches the very first line of input the 0,addr2 form will be at the end of its range, whereas the 1,addr2 form will still be at the beginning of its range. This works only when addr2 is a regular expression. addr1,+N Will match addr1 and the N lines following addr1. addr1,~N Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.

!

表示匹配成功后是否执行命令

cmd

要执行的命令。

下面摘录一下sed man文档中的常用命令(其中删除了较复杂的与label有关的命令)

不可接受address的命令

1

} The closing bracket of a { } block.

可接受零个或一个address的命令

123456789101112131415

= Print the current line number.a \text Append text, which has each embedded newline preceded by a backslash.i \text Insert text, which has each embedded newline preceded by a backslash.r filename Append text read from filename.R filename Append a line read from filename. Each invocation of the command reads a line from the file. This is a GNU extension.

可接受address范围的命令

1234567891011121314151617181920212223242526272829303132333435

{ Begin a block of commands (end with a }).c \text Replace the selected lines with text, which has each embedded newline preceded by a backslash.d Delete pattern space. Start next cycle.D If pattern space contains no newline, start a normal new cycle as if the d command was issued. Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the resul- tant pattern space, without reading a new line of input.h H Copy/append pattern space to hold space.g G Copy/append hold space to pattern space.n N Read/append the next line of input into the pattern space.p Print the current pattern space.P Print up to the first embedded newline of the current pattern space.s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.w filename Write the current pattern space to filename.W filename Write the first line of the current pattern space to filename. This is a GNU extension.x Exchange the contents of the hold and pattern spaces.y/source/dest/ Transliterate the characters in the pattern space which appear in source to the corresponding character in dest.

常用命令解析

sed -n '1p' test.txt

打印第一行,这条命令其实应该理解为sed -n '1 p' test.txt, 其中1是一个address,这条命令实际是说按照address的说明,仅第一行被作为要操作的address范围,那么在这个范围里每一行就执行p命令,同时-n说明不要把处理的模式空间内容打印出来,于是最后就打印了第一行。

sed -i 's/abcd/efgh/g' test.txt

将文件中所有的abcd替换成efgh,这条命令没有address范围,那么address范围默认就是整个文件范围,这里对整个文件范围里每一行执行s/abcd/efgh/g命令,即将每一行里的abcd替换成efgh, 同时因为有/g选项,一行里如果出现多个abcd, 就每一个都会替换。-i参数说明将直接修改文件,而不仅仅将结果打印到标准输出里(注意MAC OSX下要达成相同效果要写-i '')。

sed '{/This/{/fish/d}}' test.txt

删除文件中即有This也有fish的行,这条命令没有address范围,那么address范围默认就是整个文件范围,这里对整个文件范围里每一行执行{/This/{/fish/d}}命令,这是个嵌套命令,意思是先匹配/This/,匹配成功的行再尝试匹配/fish/,如果又匹配成功,则删除该行。

sed '{/This/d; /fish/d}' test.txt

删除文件中有Thisfish的行,这条命令与上面那条很像,但逻辑很不一样。这条命令没有address范围,那么address范围默认就是整个文件范围,这里对整个文件范围里每一行执行{/This/d; /fish/d}命令,这也是个嵌套命令,意思是针对当前行,先匹配/This/,如果匹配成功,则删除该行,否则再尝试匹配/fish/,如果匹配成功,则删除该行。

sed -i '/abcd/,/efgh/ s/xxx/yyyy/g' test.txt

这条命令就很好理解了,它有address范围,在文件里先匹配/abcd/,以匹配的行为范围的起点,再在文件里匹配/efgh/,以匹配的行为范围的终点,在这个范围里执行s/xxx/yyyy/g命令,这个就不用再解释了。

小结

还有一些关于hold space的高级用法,我平时没怎么用到,不过只要头脑里有pattern space与hold space的概念,按sed核心的执行逻辑推演一下,还是可以看懂那些高级用法的,至于要熟练运用的话就得靠多练了。附上sed常用命令及中文解释

PS

MAC OSX里记得需要使用brew install gnu-sed安装GNU版的sed,然后使用gsed, 自带的BSD版本sed功能实在弱了点。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【数据处理】sed原理及使用举例(快速理解核心)
在做数据开发中,经常需要通过shell脚本/命令来针对文本进行预处理,sed是一个很强大的流式处理命令,笔者几乎每天都会用到,在这统一梳理总结了下! 其实 sed 很简单,比vim简单很多了! 1. 基础 核心概念 两个空间: 模式空间(pattern space); 交换空间(hold space 保持空间) 模式空间:容纳当前行的缓冲区,即通过模式匹配到的行被读入该空间中 保持空间:一个辅助缓冲区,可以和模式空间进行交互(通过h,H,g,G),但命令不能直接作用于该空间,在进行数据处理时作为“暂存区域”
onephone
2022/03/30
3.3K5
【数据处理】sed原理及使用举例(快速理解核心)
Sed 命令详解
sed是stream editor的简称,也就是流编辑器。它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。
Allen Cheng
2018/09/10
9990
Sed 命令详解
运维工作中sed常规操作命令梳理
sed是一个流编辑器(stream editor),一个非交互式的行编辑器。它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕;接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。在日常的运维工程中,会时常用sed命令来处理行操作,下面根据工作中的使用经验对sed的用法做一梳理: sed(stre
洗尽了浮华
2018/01/22
1.1K0
运维工作中sed常规操作命令梳理
sed的基本用法详解
在Linux的世界中,有着一个文本三剑客的称呼,它们分别代表grep(文本过滤),sed(流编辑器),awk(gawk)(报告生成器)。 它们是强大的文本处理工具,了解并掌握它们,可以让你对文本的处理更加从容和轻松。 今天我们主要是围绕sed来进行分析。 一、初识sed sed:Stream Editor 从名字上也可以直观的了解到它是一个流编辑工具。何为流编辑器?就是把文本中的文字按照特定的分隔方式,进行数据流处理。sed就是基于这种方式,它是以换行符以分隔单位,对文本进行逐行的处理。 ---- 二、
小小科
2018/05/02
2.5K0
sed的基本用法详解
SED 命令简明教程
awk于 1977 年出生,今年 36 岁本命年,sed比awk大 2-3 岁,awk就像林妹妹,sed 就是宝玉哥哥了。所以 林妹妹跳了个Topless,他的哥哥sed坐不住了,也一定要出来抖一抖。
ihoey
2018/10/31
8860
一天一个 Linux 命令(19):grep 命令
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/148
joshua317
2021/09/29
4450
一天一个 Linux 命令(20):sed 命令
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/150
joshua317
2021/10/09
3170
详解流编辑器 sed 和 编程语言 awk
本文介绍了如何使用流编辑器sed和编程语言awk对文本进行编辑和处理。首先介绍了流编辑器sed,它是一种非交互式编辑器,可以用于删除、替换和插入文本。然后介绍了编程语言awk,它是一种功能强大的文本处理工具,可以用于各种文本处理任务,包括从文本中提取信息、过滤和转换文本以及处理文本数据。
s1mba
2017/12/28
1.2K0
详解流编辑器 sed 和 编程语言 awk
linux中sed命令总结
原文:https://wangchujiang.com/linux-command/c/sed.html
入门笔记
2022/06/02
3.4K0
sed入门详解教程 原
    sed 是一个比较古老的,功能十分强大的用于文本处理的流编辑器,加上正则表达式的支持,可以进行大量的复杂的文本编辑操作。sed 本身是一个非常复杂的工具,有专门的书籍讲解 sed 的具体用法,但是个人觉得没有必要去学习它的每个细节,那样没有特别大的实际意义。网上也有很多关于 sed 的教程,我也是抱着学习的心态来学习 sed 的常见的用法,并进行系统的总结,内容基本覆盖了 sed 的大部分的知识点。文中的内容比较简练,加以实际示例来帮助去理解 sed 的使用。
拓荒者
2019/03/11
1.5K0
sed入门详解教程
                                                                            原
linux基础命令介绍十:文本流编辑 sed
与vim不同,sed是一种非交互式的文本编辑器,同时它又是面向字符流的,每行数据经过sed处理后输出。
用户5030870
2019/04/10
1.1K0
sed & awk 第二版学习(五)—— 高级 sed 命令
高级命令改变执行或控制的流程顺序。sed 脚本中正常的控制流为:一行被读入模式空间并用脚本中的每个命令逐个应用于那一行;当到达脚本底部时,输出这一行并且清空模式空间;然后新行被读入模式空间,并且控制被转移回脚本顶端。
用户1148526
2024/09/20
1810
第二十一章 : 文本处理
All Unix-like operating systems rely heavily on text files for several types of datastorage. So it makes sense that there are many tools for manipulating text. In thischapter, we will look at programs that are used to “slice and dice” text. In the nextchapter, we will look at more text processing, focusing on programs that are used toformat text for printing and other kinds of human consumption.
砖业洋__
2023/05/06
6190
Linux之sed命令详解
语法 sed [-hnV][-e<script>][-f<script文件>][文本文件]
AsiaYe
2019/11/06
3.2K0
Linux之sed命令详解
Linux 流编辑器 sed 详解
Linux 中,常使用流编辑器 sed 进行文本替换工作。与常使用的交互式编辑器(如vim)不同,sed 编辑器以批处理的方式来编辑文件,这比交互式编辑器快得多,可以快速完成对数据的编辑修改。
用户6543014
2019/10/25
1.5K0
Linux 流编辑器 sed 详解
awk(报告生成器),grep(文本过滤器),sed(流编辑器)使用入门
三剑客 linux下的文本三剑客 grep egrep,grep,fgrep 文本查找的需要 grep:根据模式搜索文本,并将符合模式的文本行显示出来。 pattern:文本符和正则表达式的元字符组合而成的匹配条件 grep [option] "pattern" file grep root /etc/passwd -i:忽略大小写 --color:匹配的字符高亮显示 alias alias grep='grep --color' -v:反向查找 -o:只显示被模式匹配的字符串(不显示行
若与
2018/04/25
1.4K0
awk(报告生成器),grep(文本过滤器),sed(流编辑器)使用入门
Sed命令的基本使用
该文介绍了sed命令的基本使用,包括打印匹配行、替换匹配行、从文件中读取并写入到输出中等常用用法,以及使用修饰符进行更高级的文本处理。同时还介绍了sed命令的常用选项和例子,以及与其他文本处理工具的对比。
GavinZhou
2018/01/02
1.2K0
详细grep、sed、awk
[root@VM_0_7_centos tmp]# cat 1.txt 1 2 3 4 5 6 [root@VM_0_7_centos tmp]# cat 2.txt 4 5 6 7 8 [root@VM_0_7_centos tmp]# grep -f 1.txt 2.txt 4 5 6 [root@VM_0_7_centos tmp]# grep -f -v 1.txt 2.txt grep: -v: No such file or directory [root@VM_0_7_centos
用户1173509
2018/03/28
1.7K0
详细grep、sed、awk
sed命令扩展–转载
文本处理工具之二 sed命令详解 sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器。能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上。还可以对原文件改动,但是不会再屏幕上返回结果。 sed命令的语法格式: sed的命令格式: sed [option] ‘sed command’filename sed命令的选项(option): -n :只打印模式匹配的行 -e :直接在命令行模式上进行sed动作编辑,此为
老七Linux
2018/05/09
9060
Linux||sed命令使用讲解
sed是stream editor的缩写,译为"流编辑器",一般用来对文本数据进行增删改查即新增、删除、替换、查找。
小汪Waud
2023/02/03
1.4K0
Linux||sed命令使用讲解
相关推荐
【数据处理】sed原理及使用举例(快速理解核心)
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验