sed介绍
sed全称(stream editor)流式编辑器,Sed主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等,工作流程如下
1 2 |
sed 是一种在线的、非交互式的编辑器,它一次处理一行内容。处理时,把当前处理的行存储在 临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完 成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出,或者使用sed -i选项 |
sed命令格式
1 2 3 4 5 6 7 |
sed [options] 'command' file(s) sed [options] -f scriptfile file(s) sed -选项 '/正则/动作' 文件 # 注: sed和grep不一样,不管是否找到指定的模式,它的退出状态都是0 只有当命令存在语法错误时,sed的退出状态才不是0 |
常用选项
选项 | 说明 |
---|---|
-e | 允许多项编辑 |
-n | 取消默认输出 |
-r | 使用扩展正则表达式 |
-i | 原地编辑(修改源文件) |
-f | 指定sed脚本的文件名 |
-d | 过滤 排除 |
常见处理动作
动作 | 说明 |
---|---|
'p' | 打印 |
'i' | 在指定行之后插入内容 |
'a' | 在指定行之后插入内容 |
'c' | 替换指定行所有内容 |
'd' | 删除指定行 |
打印文件内容
sed -选项 '/正则/动作' 文件
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@lb01 ~]# sed '' a.txt # 对文件什么都不做,流出每一行 [root@lb01 ~]# sed -n 'p' a.txt # 打印每一行,并取消默认输出 [root@lb01 ~]# sed -n '1p' a.txt # 打印第一行 [root@lb01 ~]# sed -n '1,5p' a.txt # 打印1到5行 [root@lb01 ~]# sed -n '$p' a.txt # 打印最后1行 [root@lb01 ~]# sed -nr '2,$p' a.txt # 打印2行到结尾 [root@lb01 ~]# sed -nr '/^clf.*clf$/p' a.txt # 打印以clf开头且以clf结尾的行 [root@lb01 ~]# sed -nr '/d/p' a.txt # 打印匹配包含d的行 [root@lb01 ~]# sed -r '3,/n$/d' a.txt # 删除3到下一个n结尾的行(默认不贪婪) [root@lb01 ~]# sed -r '1,3{p;d}' a.txt # 虽然和正常结果一样但是1到3行是p打印的而不是默认流出的 [root@lb01 ~]# sed -r '1,3{s#egon#XXX#g;3q}' a.txt # 替换1到3行退出 |
增加文件内容
i 地址定位的上面插入
a 下面插入
1 2 3 4 5 6 7 8 9 10 |
[root@lb02 ~]# sed '$a clf' a.txt # 文件最后一行下面增加内容 [root@lb02 ~]# sed 'a clf' a.txt # 文件每行下面增加内容 [root@lb02 ~]# sed '5a clf' a.txt # 文件第5行下面增加内容 [root@lb02 ~]# sed '$i clf' a.txt # 文件最后一行上一行增加内容 [root@lb02 ~]# sed 'i clf' a.txt # 文件每行上一行增加内容 [root@lb02 ~]# sed '6i clf' a.txt # 文件第6行上一行增加内容 [root@lb02 ~]# sed '/^uucp/ihello' # 以uucp开头行的上一行插入内容 [root@lb02 ~]# sed -r '4r nginx.conf' a.txt # 将nginx.conf文件加到a.txt的第4行后面 [root@lb02 ~]# sed -r '1,3w /tmp/nginx.conf' a.txt # 将1到3行的a.txt写入到nginx.conf文件中 |
修改文件内容
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@lb01 ~]# sed '5chello world' a.txt # 替换文件第5行内容 [root@lb01 ~]# sed 'chello world' a.txt # 替换文件所有内容 [root@lb01 ~]# sed '1,5chello world' a.txt # #替换文件1到5号内容为hello world [root@lb01 ~]# sed -r '1,3s#clf#CLF#g' a.txt # 将1到3行的clf替换为CLF [root@lb01 ~]# sed -r '/^clf/s#111#222#g' a.txt # 匹配以clf开头的行将111替换为222 [root@lb01 ~]# sed -r '/^clf/s#^111#222#g' a.txt # 匹配以clf开头的行只将开头的111替换为222 [root@lb01 ~]# sed -r 's#clf#CLF#gi' a.txt # 将的clf(忽略大小写)替换为CLF [root@lb01 ~]# sed -r 's/[0-9]$/&.change/' a.txt # 将以数字结尾的行追加.change [root@lb01 ~]# sed -r 's#([a-zA-Z]+)([^a-zA-Z]+)#\2\1#' a.txt # 将连续的字母和数字对调 [root@lb01 ~]# sed -nr '1,5{p;s#clf#egon#g}' a.txt # 将1到5行的clf替换为egon [root@lb02 ~]# sed -r 'y/elo/XYZ/' a.txt # 将e替换为X 将l替换为Y 将o替换为Z |
删除文件内容
1 2 3 4 5 6 7 8 9 10 |
[root@lb01 ~]# sed '1d' a.txt #删除文件第1行 [root@lb01 ~]# sed '1,5d' a.txt #删除文件1到5行 [root@lb01 ~]# sed '1,5!d' a.txt #删除文件1到5行以外的 [root@lb01 ~]# sed '$d' a.txt #删除文件最后一行 [root@lb01 ~]# sed '1d;3d;5d' a.txt #删除1 3 5行 [root@lb01 ~]# sed '5,$d' a.txt #删除5行到结尾 [root@lb01 ~]# sed '/clf/d' a.txt #删除包含clf的行 [root@lb01 ~]# sed '/clf$/d' a.txt #删除以clf的行 [root@lb01 ~]# sed '/^clf.*clf$/d' a.txt #删除以clf开头和以clf结尾的行 |
模式空间与保持空间
sed 有两个内置的存储空间:
1 2 3 4 |
● 模式空间(pattern space): 模式空间用于 sed 执行的正常流程中。该空间 sed 内置的一个缓冲区,用来存放、修改从输入文件读取的内容。 ● 保持空间(hold space): 保持空间是另外一个缓冲区,用来存放临时数据。Sed 可以在保持空间和模式空间交换数据,但是不能在保持空间上执行普通的 sed 命令。 |
模式空间与保持空间的操作命令
命令 | 详解 |
---|---|
x | 命令x(exchange) 用于交换模式空间和保持空间的内容 |
h | 模式空间复制/覆盖到保持空间 |
H | 模式空间追加到保持空间 |
g | 保持空间复制/覆盖到模式空间 |
G | 保持空间追加到模式空间 |
n | 读取下一行到/覆盖到模式空间 |
N | 将下一行添加到模式空间 |
d | 删除pattern space中的所有行,并读入下一新行到pattern space中 |
交换文件的行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[root@egon ~]# cat test.txt 1111 2222 3333 # ======================方式1:====================== [root@egon ~]# tac test.txt 3333 2222 1111 # ======================方式2:====================== # 1、读取文件第一行内容到模式空间,进行的操作如下 # 将模式空间内容覆盖到保持空间 # 删除模式空间内容 # 2、读取文件第二行内容到模式空间,进行的操作如下 # 将保持内容追加到模式空间 # 将模式空间内容覆盖到保持空间 # 删除模式空间内容 # 3、读取文件第三行内容到模式空间,进行的操作如下 # 将保持空间内容追加到模式空间 sed -r '1h;1d;2G;2h;2d;3G' test.txt |