Liunx sed

概述

sed是stream editor的简称,也就是流编辑器。它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。

使用语法

sed命令的使用规则是这样的:

1
sed [option] 'command' input_file

其中option是可选的,常用的option有如下几种:

  • -n 使用安静(silent)模式(想不通为什么不是-s)。在一般sed的用法中,所有来自stdin的内容一般都会被列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者动作)才会被列出来;
  • -e 直接在指令列模式上进行 sed 的动作编辑;
  • -f 直接将 sed 的动作写在一个文件内, -f filename 则可以执行filename内的sed命令;
  • -r 让sed命令支持扩展的正则表达式(默认是基础正则表达式);
  • -i 直接修改读取的文件内容,而不是由屏幕输出。

    常用的命令有以下几种:

  • a \: append即追加字符串, a \的后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选择的行的后面都加上字符串s;

  • c \: 取代/替换字符串,c \后面跟上字符串s(多行字符串可以用\n分隔),则会将当前选中的行替换成字符串s;
  • d: delete即删除,该命令会将当前选中的行删除;
  • i \: insert即插入字符串,i \后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选中的行的前面都插入字符串s;
  • p: print即打印,该命令会打印当前选择的行到屏幕上;
  • s: 替换,通常s命令的用法是这样的:1,2s/old/new/g,将old字符串替换成new字符串

命令示例

假设有一个本地文件test.txt,文件内容如下:

[qifuguang@winwill~]$ cat test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end

本节将使用该文件详细演示每一个命令的用法。

a命令

1
2
3
4
5
6
7
8
9
[qifuguang@winwill~]$ sed '1a \add one' test.txt
this is first line
add one
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
end

本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用$表示最后一行,比如2,$表示第二行到最后一行中间所有的行(包含第二行和最后一行)。
本例的作用是在第一行之后增加字符串”add one”,从输出可以看到具体效果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[qifuguang@winwill~]$ sed '1,$a \add one' test.txt
this is first line
add one
this is second line
add one
this is third line
add one
this is fourth line
add one
this is fifth line
add one
happy everyday
add one
end
add one

本例表示在第一行和最后一行所有的行后面都加上”add one”字符串,从输出可以看到效果。

1
2
3
4
5
6
7
8
9
[qifuguang@winwill~]$ sed '/first/a \add one' test.txt
this is first line
add one
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
end

本例表示在包含”first”字符串的行的后面加上字符串”add one”,从输出可以看到第一行包含first,所以第一行之后增加了”add one”

1
2
3
4
5
6
7
8
9
[qifuguang@winwill~]$ sed '/^ha.*day$/a \add one' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is fifth line
happy everyday
add one
end

本例使用正则表达式匹配行,^ha.*day$表示以ha开头,以day结尾的行,则可以匹配到文件的”happy everyday”这样,所以在该行后面增加了”add one”字符串。

i命令

i命令使用方法和a命令一样的,只不过是在匹配的行的前面插入字符串,所以直接将上面a命令的示例的a替换成i即可,在此就不啰嗦了。

c命令

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed '$c \add one' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is     fifth line
happy everyday
add one

本例表示将最后一行替换成字符串”add one”,从输出可以看到效果。

1
2
3
4
5
[qifuguang@winwill~]$ sed '4,$c \add one' test.txt
this is first line
this is second line
this is third line
add one

本例将第四行到最后一行的内容替换成字符串”add one”。

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed '/^ha.*day$/c \replace line' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is fifth line
replace line
end

本例将以ha开头,以day结尾的行替换成”replace line”。

d命令

1
2
3
4
5
6
7
[qifuguang@winwill~]$ sed '/^ha.*day$/d' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is fifth line
end

本例删除以ha开头,以day结尾的行。

1
2
3
4
[qifuguang@winwill~]$ sed '4,$d' test.txt
this is first line
this is second line
this is third line

本例删除第四行到最后一行中的内容。

p命令

1
2
3
4
5
[qifuguang@winwill~]$ sed -n '4,$p' test.txt
this is fourth line
this is fifth line
happy everyday
end

本例在屏幕上打印第四行到最后一行的内容,p命令一般和-n选项一起使用。

1
2
[qifuguang@winwill~]$ sed -n '/^ha.*day$/p' test.txt
happy everyday

本例打印以ha开始,以day结尾的行。

s命令

实际运用中s命令式最常使用到的。

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed 's/line/text/g' test.txt
this is first text
this is second text
this is third text
this is fourth text
this is fifth text
happy everyday
end

本例将文件中的所有line替换成text,最后的g是global的意思,也就是全局替换,如果不加g,则只会替换本行的第一个line。

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed '/^ha.*day$/s/happy/very happy/g' test.txt
this is first line
this is second line
this is third line
this is fourth line
this is fifth line
very happy everyday
end

本例首先匹配以ha开始,以day结尾的行,本例中匹配到的行是”happy everyday”这样,然后再将该行中的happy替换成very happy。

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed 's/\(.*\)line$/\1/g' test.txt
this is first
this is second
this is third
this is fourth
this is fifth
happy everyday
end

这个例子有点复杂,先分解一下。首先s命令的模式是s/old/new/g这样的,所以本例的old部分即\(.*\)line$,sed命令中使用\(\)包裹的内容表示正则表达式的第n部分,序号从1开始计算,本例中只有一个\(\)所以\(.*\)表示正则表达式的第一部分,这部分匹配任意字符串,所以\(.*\)line$匹配的就是以line结尾的任何行。然后将匹配到的行替换成正则表达式的第一部分(本例中相当于删除line部分),使用\1表示匹配到的第一部分,同样\2表示第二部分,\3表示第三部分,可以依次这样引用。比如下面的例子:

1
2
3
4
5
6
7
8
[qifuguang@winwill~]$ sed 's/\(.*\)is\(.*\)line/\1\2/g' test.txt
this  first
this  second
this  third
this  fourth
this  fifth
happy everyday
end

正则表达式中is两边的部分可以用\1\2表示,该例子的作用其实就是删除中间部分的is。

声明

本文为作者原创,转载请注明出处,本文链接:http://qifuguang.me/2015/09/21/sed命令详解/

### 关于 Linux 中 `sed` 命令的使用方法 #### 什么是 `sed`? `sed` 是一种流编辑器 (stream editor),主要用于自动化文本处理任务。它能够读取输入流并对其进行过滤和转换,最终将结果输出到标准输出设备或保存至文件中[^1]。 #### 安装与版本确认 大多数现代 Linux 发行版默认已安装 GNU 版本的 `sed` 工具。可以通过以下命令验证其是否存在以及当前使用的版本号: ```bash sed --version ``` 如果未找到该工具,则可以依据具体发行版通过包管理器进行安装。例如,在基于 Debian 的系统上运行: ```bash sudo apt-get install sed ``` 对于 Red Hat 或 CentOS 系统,执行: ```bash sudo yum install sed ``` #### 基础语法结构 `sed` 的基本形式如下所示: ```bash sed OPTIONS... {SCRIPT} [INPUTFILE]... ``` 其中 `{SCRIPT}` 表达了要应用的一系列指令集合;而 `[INPUTFILE]` 参数表示待处理的目标文件路径列表。如果没有提供任何显式的输入文件名,默认会从标准输入获取数据流来作为操作对象[^5]。 #### 实际应用场景举例说明 ##### 替换字符串实例 假设有一个名为 example.txt 文件内容如下: ``` apple banana orange applepie ``` 我们希望把所有的 “apple” 字样替换成 “fruit”,那么可以用下面这条命令实现这一需求: ```bash sed 's/apple/fruit/g' example.txt ``` 这里 `'s/pattern/replacement/'` 构成了替换动作的核心部分,“g” 标志意味着全局范围内的查找替换活动将会被执行而不是仅仅局限于每行中的首次出现位置处完成相应更改工作而已[^2]。 另外需要注意的是当涉及到多字符模式匹配时可能需要用到正则表达式技术进一步增强灵活性与精确度等方面表现效果更佳的情况之下再考虑引入相关内容知识点即可满足实际开发过程中遇到的各种复杂场景下的不同层次要求水平之间相互协调配合从而达到最佳实践方案设计思路方向上去努力奋斗成就伟大事业梦想成真之路越走越宽广无限美好未来等着大家一起去探索发现创造奇迹时刻来临之际让我们共同见证历史性的突破进展吧朋友们加油干起来啊!!! ##### 同时创建副本功能展示 有时候除了想看到即时屏幕上的变化之外还希望能够保留原始文档不变的同时生成一个新的经过修改后的版本存盘下来供后续查阅分析对比研究之用的话就可以借助 tee 命令联合行动达成目的啦比如这样子的操作方式就很不错哦亲测有效哈😊: ```bash sed 's/error/warning/' inputfile | tee outputfile ``` 上述例子当中我们将原错误提示信息调整为了警告级别显示样式之后不但能够在终端窗口里立即观察得到最新状态而且还额外建立起了一个叫做outputfile的新档案专门用来存储这些更新过的内容资料以便日后随时调阅参考利用价值非常高呢??[^3]. 最后提醒一点就是记得一定要仔细检查各自平台环境下所支持的具体参数选项含义解释说明文档以免误操作造成不必要的麻烦困扰哟😎. ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值