什么是sed?

Linux中的命令主要是操作文件或字符串,对字符串进行匹配、修改、替换是非常常见的操作。sed作为Shell三剑客之一,对文件中的字符串处理尤其擅长,不仅可以将修改后的结果在标准输出显示,还可以直接替换修改原始文件。

linux系统下常用的文本编辑(LinuxShell三剑客之sed功能强大的文本处理工具)(1)

sed的工作流程

linux系统下常用的文本编辑(LinuxShell三剑客之sed功能强大的文本处理工具)(2)

语法格式

sed [-nefri] 'command' 输入文本

常用选项:

-e 进行多项编辑,即对输入行应用多条sed命令时使用 -n 取消默认的输出 -f 指定sed脚本的文件名 -r 使用扩展正则表达式 -i inplace,原地编辑(修改源文件)

命令选项:

p 打印匹配行 = 显示文件行号 a/ 在定位行号后附加新文本信息 i/ 在定位行号后插入新文本信息 d 删除定位行 c/ 用新文本替换定位文本 s 使用替换模式替换相应模式 r 从另一个文本中读文本 w 写文本到一个文件

打印行p 删除行d

sed -n '1,5p' input.txt 打印1-5行 sed -n '$p' input.txt 打印最后一行 sed -n '/root/p' input.txt 打印包括root的整行 sed '1,5d' input.txt 删除1-5行 sed '/root/d' input.txt 删除包括root的行

!取反所选行:

hioier@yunpc:~/scripts$ sed -n '1,3!p' input.txt daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

搜索替换

linux系统下常用的文本编辑(LinuxShell三剑客之sed功能强大的文本处理工具)(3)

行首添加注释

linux系统下常用的文本编辑(LinuxShell三剑客之sed功能强大的文本处理工具)(4)

行首行尾同时替换:

sed -n '1,3s/^/# &/g;1,3s/$/& >>/gp' input.txt

插入行和替换行

i\ 在当前行之前插入文本。多行时除最后一行外,每行末尾需用"\"续行 a\ 在当前行后添加一行或多行。多行时除最后一行外,每行末尾需用“\”续行 c\ 用此符号后的新文本替换当前行中的文本。多行时除最后一行外,每行末尾需用"\"续行,整行替换

hioier@yunpc:~/scripts$ sed '/root/a\hioier.com' input.txt root:x:0:0:root:/root:/bin/bash hioier.com root:x:0:0:root:/root:/bin/bash hioier.com root:x:0:0:root:/root:/bin/bash hioier.com daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin hioier@yunpc:~/scripts$ sed '/root/a\hioier.com\nhello cat.' input.txt root:x:0:0:root:/root:/bin/bash hioier.com hello cat. root:x:0:0:root:/root:/bin/bash hioier.com hello cat. root:x:0:0:root:/root:/bin/bash hioier.com hello cat.

hioier@yunpc:~/scripts$ sed '/root/c\hioier.com\ > hello cat. > ' input.txt hioier.com hello cat. hioier.com hello cat. hioier.com hello cat. daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

读入文件、写入文件

hioier@yunpc:~/scripts$ sed '2r test.txt' input.txt root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash hioier.com 1 hioier.com 2 hioier.com 3 root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

hioier@yunpc:~/scripts$ sed '/root/w output.txt' input.txt root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin hioier@yunpc:~/scripts$ cat output.txt root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash

打印行号和多项编辑

hioier@yunpc:~/scripts$ sed -n '/root/=' input.txt 1 2 3 hioier@yunpc:~/scripts$ sed -ne '/root/=' -ne '/root/p' input.txt 1 root:x:0:0:root:/root:/bin/bash 2 root:x:0:0:root:/root:/bin/bash 3 root:x:0:0:root:/root:/bin/bash hioier@yunpc:~/scripts$

sed脚本格式

sed脚本就是将多个操作命令放到一个文件中执行。

过滤.bashrc配置文件:

sed '/^#/d;/^$/d;/^\t$/d;/^\t#/d;/^.*#/d' bashrc

在文件收尾添加信息:

sed '1i\------ start ------' bashrc sed '$a\------ end ------' bashrc

sed.sh

#!/bin/sed -f s/root/hioier/g 1i\ ------- start ------ $a\ ------- end ------

sed -f sed.sh input.txt ./sed.sh -i input.txt

视频教程

相关推荐,