linux基础--sed编辑器详解

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1、sed简介

  sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

  sed默认不编辑源文件,仅对模式空间中的数据作处理。处理结束后,将模式空间打印。

2、命令语法

1
sed  [options]  'command'  file (s)

3、命令使用示例

1
2
将data.inc.php中从mysql_dlevent到mysql_activation之间的root替换成killtr
sed  -i -e  "/mysql_dlevent/,/mysql_activation/s/root/killtr/"   config /data .inc.php

4、常见options选项

-n:静默模式,只显示符合条件的行,不再默认显示模式空间中的内容。

-i:直接修改原文件

-f /PATH/TO/SED_SCRIPT:将使用的脚本保存到文件中

-r:使用扩展正则表达式

-e SCRIPT -e SCRIPT:可同时执行多个脚本

1
2
3
4
[root@liang-study scripts] # sed -e '/^#$/d' -e '/^#[[:space:]]\{3,\}/d' /etc/inittab  
id :3:initdefault:
S0:12345:respawn: /sbin/agetty  ttyS0 115200
#-e示例,删除/etc/inittab中只有#的行和以#开头后至少有3个空格的行


5、常见command参数

 d:删除指定行

1
2
3
4
5
6
7
8
9
10
11
12
[root@liang-study scripts] # sed '1,2d' /etc/passwd
daemon:x:2:2:daemon: /sbin : /sbin/nologin
adm:x:3:4:adm: /var/adm : /sbin/nologin
#删除/etc/passwd中1-2行的数据
[root@liang-study scripts] # sed '3,$d' /etc/passwd
#删除/etc/passwd中第3行到最后一行的数据
[root@liang-study scripts] # sed '/root/d' /etc/passwd
#删除包含root的行
[root@liang-study scripts] # sed '1,+2d' /etc/passwd
#删除第一行和第一行后2行的数据
[root@liang-study scripts] # sed '/^\//d' /etc/fstab 
#删除以/开头的行,注意匹配的/需要转义

p:显示指定行

1
2
3
4
5
6
7
8
[root@liang-study scripts] # sed '/^\//p' /etc/fstab  
proc                     /proc                    proc    defaults        0 0
/dev/vdb                 /chenchao                ext4    defaults,usrquota,grpquota 1 2
/dev/vdb                 /chenchao                ext4    defaults,usrquota,grpquota 1 2
#注:上例为显示以/开头的行,需要注意的是默认目标文件中匹配的行显示两次,没有匹配的行显示一次,这是因为sed默认是读一行显示一行,而p匹配到的则会单独显示,因此就会匹配到的就会显示两次。可以使用sed选项来处理。
[root@liang-study scripts] # sed -n '/^\//p' /etc/fstab 
/dev/vdb                 /chenchao                ext4    defaults,usrquota,grpquota 1 2
#使用-n选项只显示匹配到的行。

a \string:在指定的行后追加新航,内容为string。

1
2
3
4
5
6
7
8
9
[root@liang-study scripts] # sed '/^\//a \#hello liang' /etc/fstab       
/dev/vdb                 /chenchao                ext4    defaults,usrquota,grpquota 1 2
#hello liang
#在/etc/fstab中以/开头的行后增加#hello liang
[root@liang-study scripts] # sed '/^\//a \#hello liang\n#hello linux' /etc/fstab    
/dev/vdb                 /chenchao                ext4    defaults,usrquota,grpquota 1 2
#hello liang
#hello linux
#在/etc/fstab中以/开头的行后增加两行

i \strig:在指定的行前追加新航,内容为string,用法和a \string一样。

r FILE:将指定文件的内容添加到符合条件的行后。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@liang-study scripts] # sed '/chenchao/r /etc/issue' /etc/fstab
/dev/vdb                 /chenchao                ext4    defaults,usrquota,grpquota 1 2
CentOS release 6.8 (Final)
Kernel \r on an \m
#将/etc/fstab中包含chenchao字符串的行后加入/etc/issue的内容
[root@liang-study scripts] # sed '$r /etc/issue' /etc/fstab          
/dev/vdb                 /chenchao                ext4    defaults,usrquota,grpquota 1 2
CentOS release 6.8 (Final)
Kernel \r on an \m
#将/etc/fstab中最后一行后加入/etc/issue的内容
[root@liang-study scripts] # sed '2r /etc/issue' /etc/passwd 
root:x:0:0:root: /root : /bin/bash
bin:x:1:1:bin: /bin : /sbin/nologin
CentOS release 6.8 (Final)
Kernel \r on an \m
#将/etc/passwd中第二行后加入/etc/issue的内容
[root@liang-study scripts] # sed '1,2r /etc/issue' /etc/passwd 
root:x:0:0:root: /root : /bin/bash
CentOS release 6.8 (Final)
Kernel \r on an \m
bin:x:1:1:bin: /bin : /sbin/nologin
CentOS release 6.8 (Final)
Kernel \r on an \m
#在第一行和第二行后面都添加/etc/issue的内容

w FILE:将指定范围内的内容另存至指定文件中

1
2
3
4
5
6
7
8
9
10
11
[root@liang-study scripts] # sed -n '/root/w /tmp/root.txt' /etc/passwd
[root@liang-study scripts] # cat /tmp/root.txt 
root:x:0:0:root: /root : /bin/bash
operator:x:11:0:operator: /root : /sbin/nologin
#将/etc/passwd中包含root的行另存至/tmp/root.txt中。
[root@liang-study scripts] # sed -n '1,3w /tmp/root.txt' /etc/passwd      
[root@liang-study scripts] # cat /tmp/root.txt                      
root:x:0:0:root: /root : /bin/bash
bin:x:1:1:bin: /bin : /sbin/nologin
daemon:x:2:2:daemon: /sbin : /sbin/nologin
#将/etc/passwd中第一行到第三行另存至/tmp/root.txt中。

s/pattern/string/:查找并替换,pattern支持正则表达式。默认只替换每行中第一次被模式匹配到的字符串

  s/pattern/string/g:全局替换

  s/pattern/string/i:忽略字符大小写

    同时s/pattern/string/也可以使用s#pattern#string#来表示。只要保证使用分隔符相同即可。且如果使用#或者@作为分割符时,在pattern或string中遇到/则无需转义了。

1
2
3
4
5
6
7
8
9
10
11
12
[root@liang-study scripts] # sed 's/root/ROOT/' /etc/passwd
ROOT:x:0:0:root: /root : /bin/bash
#将/etc/passwd中每行第一次包含root的字符串替换成ROOT
[root@liang-study scripts] # tail -1 /etc/fstab 
/dev/vdb                 /chenchao                ext4    defaults,usrquota,grpquota 1 2
[root@liang-study scripts] # sed 's/^\//#/' /etc/fstab 
#dev/vdb                /chenchao               ext4    defaults,usrquota,grpquota 1 2
#将/etc/passwd中行首为/的字符串替换成#,此处使用到了正则表达式。
[root@liang-study scripts] # sed 's/\//#/g' /etc/passwd
root:x:0:0:root: #root:#bin#bash
bin:x:1:1:bin: #bin:#sbin#nologin
#将/etc/passwd中所有/替换成#

  &:引用模式匹配到的整个串

1
2
3
4
5
6
7
8
9
10
11
[root@liang-study scripts] # cat sed.txt                  
hello,like
hi,my love
[root@liang-study scripts] # sed 's/l..e/&r/g' sed.txt     
hello,liker
hi,my lover
#将sed.txt包含l..e的字符串都替换成原有字符串并加r。
[root@liang-study scripts] # sed 's/\(l..e\)/\1r/g' sed.txt   
hello,liker
hi,my lover
#使用后向引用的方法来完成上述替换
1
2
3
4
5
6
sed  -i -e  "/mysql_dblib_ct01/,/q/s/.*DB_PASS.*/    'DB_PASS'   =>'daleevent123!'/"   ${web_dir} /config/data .inc.php
#替换包含指定字符串到文尾的一整行内容
[root@liang-study scripts] # history |sed 's/^[[:space:]]\{1,\}//'
#将history中行首的空格删除
[root@liang-study scripts] # history |sed 's/^[[:space:]]*//' | cut -d' ' -f1
#将history中行首的空格删除并取出第一列

本文转自  亮公子  51CTO博客,原文链接:http://blog.51cto.com/iyull/1884659

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
Shell Linux C语言
【Shell 命令集合 文本编辑器 】Linux pico 编辑器使用指南
【Shell 命令集合 文本编辑器 】Linux pico 编辑器使用指南
35 1
|
1月前
|
存储 搜索推荐 Shell
【Shell 命令集合 文本编辑器】Linux joe 编辑器的使用教程
【Shell 命令集合 文本编辑器】Linux joe 编辑器的使用教程
31 0
|
1月前
|
Shell Linux C语言
【Shell 命令集合 文本编辑器】Linux jed 编辑器使用指南
【Shell 命令集合 文本编辑器】Linux jed 编辑器使用指南
28 0
|
1月前
|
Linux 编译器 开发工具
Linux:详解(yum的使用、vim编辑器命令集合以及gcc/g++编译器的使用)
Linux:详解(yum的使用、vim编辑器命令集合以及gcc/g++编译器的使用)
118 1
|
1月前
|
存储 监控 Linux
性能工具之linux三剑客awk、grep、sed详解
Linux 三剑客 awk,sed和grep 在性能领域广泛用于性能建模、性能监控及性能分析等方面,也是各大互联网公司测试岗高频面试题,中高端测试人员必备技能之一。
52 1
性能工具之linux三剑客awk、grep、sed详解
|
1月前
|
Unix Shell Linux
【Shell 命令集合 文档编辑】Linux 文本编辑器 ex命令使用指南
【Shell 命令集合 文档编辑】Linux 文本编辑器 ex命令使用指南
34 0
|
1月前
|
存储 Shell Linux
【Shell 命令集合 文档编辑】Linux 行编辑器 ed命令使用指南
【Shell 命令集合 文档编辑】Linux 行编辑器 ed命令使用指南
29 0
|
存储 Shell Linux
⭐⭐⭐【Shell 命令集合 文档编辑 】Linux 文件内容处理 sed命令使用指南
⭐⭐⭐【Shell 命令集合 文档编辑 】Linux 文件内容处理 sed命令使用指南
26 0
|
1月前
|
IDE Linux 开发工具
【Linux】| Linux编辑器-vim的使用
【Linux】| Linux编辑器-vim的使用
|
29天前
|
存储 Linux 编译器
vim编辑器和gcc/g++编辑器的使用讲解
vim编辑器和gcc/g++编辑器的使用讲解
48 2