Shell 之 Linux运维三剑客sed
Linux运维三剑客之 sed
sed 是一个 “非交互式的” 面向字符流的编辑器。
命令行语法:
sed [options] script filename
sed 常用的命令
| sed 命令 | 作用 |
|---|---|
| a | 在匹配行后面加入文本 |
| c | 字符替换 |
| d | 删除行 |
| D | 删除第一行 |
| i | 在匹配行前面加入文本 |
| h | 复制模板块的内容到存储空间 |
| H | 追加模板块的内容到存储空间 |
| g | 将存储空间的内容复制到模式空间 |
| G | 将存储空间的内容追加到模式空间 |
| n | 读取下一个输入行,用下一个命令处理新的行 |
| N | 追加下一个输入行到莫板块后并在二者间插入新行 |
| p | 打印匹配的行 |
| P | 打印匹配的第一行 |
| q | 退出 sed |
| r | 从外部文件中读取文本 |
| w | 追加写文件 |
| ! | 匹配的逆 |
| s/old/new | 用 new 替换正则表达式 old |
| = | 打印当前行号 |
sed 常用参数
| sed 参数 | 作用 |
|---|---|
| -e | 多条件编辑 |
| -h | 帮助信息 |
| -n | 不输出没有匹配的行 |
| -f | 指定 sed 脚本 |
| -V | 版本信息 |
| -i | 直接修改原文件 |
示例
准备一段 sed.txt 文本内容如下
[root@ansible shell]# cat sed.txt
this is line 1, this is First line
this is line 2, this is Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
- 删除
使用 d 命令删除指定的行
# 删除 1-3 行
[root@ansible shell]# sed '1,3d' sed.txt
this is line 4, this is Third line
this is line 5, this is Fifth line
# 删除第1行到最后一行,即清空文件内容
[root@ansible shell]# sed '1,$d' sed.txt
# 删除最后一行
[root@ansible shell]# sed '$d' sed.txt
this is line 1, this is First line
this is line 2, this is Second line, Empty line followed
# 只保留第5行
[root@ansible shell]# sed '5!d' sed.txt
this is line 5, this is Fifth line
this is line 4, this is Third line
# 只保留第1-2行
[root@ansible shell]# sed '1,2!d' sed.txt
this is line 1, this is First line
this is line 2, this is Second line, Empty line followed
# 删除所有含 Empty 的行
[root@ansible shell]# sed '/Empty/d' sed.txt
this is line 1, this is First line
this is line 4, this is Third line
this is line 5, this is Fifth line
# 删除空行
[root@ansible shell]# sed '/^$/d' sed.txt
this is line 1, this is First line
this is line 2, this is Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
- 查找替换
使用 s 命令可将查找到的匹配文本内容替换为新的文本
s 命令利用 g 选项,可以完成所有匹配值的替换,即全局替换
注意:每一行只有第一个 line 被替换了,默认情况下之替换第一次匹配到的内容
[root@ansible shell]# sed -e 's/this/That/g' -e 's/line/LINE/g' sed.txt
That is LINE 1, That is First LINE
That is LINE 2, That is Second LINE, Empty LINE followed
That is LINE 4, That is Third LINE
That is LINE 5, That is Fifth LINE
# 或者
[root@ansible shell]# sed 's/this/That/g; s/line/LINE/g' sed.txt
That is LINE 1, That is First LINE
That is LINE 2, That is Second LINE, Empty LINE followed
That is LINE 4, That is Third LINE
That is LINE 5, That is Fifth LINE
# 匹配每一行中第二次匹配到的line,改为LINE
[root@ansible shell]# sed 's/line/LINE/2' sed.txt
this is line 1, this is First LINE
this is line 2, this is Second LINE, Empty line followed
this is line 4, this is Third LINE
this is line 5, this is Fifth LINE
# 只替换开头的 this 为 that
[root@ansible shell]# sed 's/^this/that/g' sed.txt
that is line 1, this is First line
that is line 2, this is Second line, Empty line followed
that is line 4, this is Third line
that is line 5, this is Fifth line
- 字符替换
使用 y 命令进行字符转换,其作用为将一系列字符逐个地换为另一个字符,
# 将文本中的 1234 逐个替换为 ABCD
[root@ansible shell]# sed 'y/1234/ABCD/' sed.txt
this is line A, this is First line
this is line B, this is Second line, Empty line followed
this is line D, this is Third line
this is line 5, this is Fifth line
- 插入文本
使用 i 或 a 命令插入文本,其中 i 代表在匹配行之前插入,而 a 代表在匹配行之后插入
# 在第二行之前插入 Insert
[root@ansible shell]# sed '2 i Insert' sed.txt
this is line 1, this is First line
Insert
this is line 2, this is Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
# 在第二行之后插入 Insert
[root@ansible shell]# sed '2 a Insert' sed.txt
this is line 1, this is First line
this is line 2, this is Second line, Empty line followed
Insert
this is line 4, this is Third line
this is line 5, this is Fifth line
# 在匹配的上一行中插入
[root@ansible shell]# sed '/Second/i\Insert' sed.txt
this is line 1, this is First line
Insert
this is line 2, this is Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
- 读入文本
使用 r 命令可从其他文件中读取文本,并插入匹配行之后
# 将 /etc/passwd 中的内容读出放到 sed.txt 空行之后
[root@ansible shell]# sed '/^$/r /etc/passwd' sed.txt
this is line 1, this is First line
this is line 2, this is Second line, Empty line followed
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
this is line 4, this is Third line
this is line 5, this is Fifth line
- 打印
使用 p 命令可进行打印,这里使用 sed 命令时一定要加 -n 参数,表示不打印没关系的行。
# 打印出文件中指定的行
[root@ansible shell]# sed -n '1p' sed.txt
this is line 1, this is First line
# 将 Second 替换成 second
[root@ansible shell]# sed 's/Second/second/' sed.txt
this is line 1, this is First line
this is line 2, this is second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
# 使用 p 命令,则只打印处理过的行,简化了输出(使用 -n 参数)
[root@ansible shell]# sed -n 's/Second/second/p' sed.txt
this is line 2, this is second line, Empty line followed
- 写文件
sed 本身默认并不改写元文件,而只是对缓冲区的文本做了修改并输出到屏幕;如果想将修改的文件保存,则需要使用 -i参数;还可以使用 w 命令将结果保存到外部指定文件。
# 将第一行和第二行新写入到output文件中
[root@ansible shell]# sed -n '1,2 w output' sed.txt
[root@ansible shell]# cat output
this is line 1, this is First line
this is line 2, this is Second line, Empty line followed
- sed 脚本
在平时工作中,可能需要定期对一些文件作分析操作,这种例行的工作往往有一种"标准化"的操作,例如取出文件中的空行,然后再全部替换某些字符等。
事实上,可以把这些动作静态化地写到某个文件中,然后调用 sed 命令并使用 -f 参数指定该文件。如下。
# 定义一个 sed.rules 的脚本,将全文的 this 改为 THAT,并删除所有空行
[root@ansible shell]# cat sed.relues
s/this/THAT/g
/^$/d
# 使用 -f 参数指定该脚本并应用于 sed.txt
[root@ansible shell]# sed -f sed.relues sed.txt
THAT is line 1, THAT is First line
THAT is line 2, THAT is Second line, Empty line followed
THAT is line 4, THAT is Third line
THAT is line 5, THAT is Fifth line
- 高级替换
替换匹配行的下一行
# 匹配空行的下一行的 line 替换成 LINE
[root@ansible shell]# sed '/^$/{n;s/line/LINE/g}' sed.txt
this is line 1, this is First line
this is line 2, this is Second line, Empty line followed
this is LINE 4, this is Third LINE
this is line 5, this is Fifth line
将模式空间的内容放入存储空间以便于接下来的编辑
实现该功能就要引入 H/h/G/g 这4个命令了。
模式空间:当前输入行的缓冲区
存储空间:模式空间以外的一个预留缓冲区
H:将模式空间的内容追加到存储空间
h:将模式空间的内容复制到存储空间,覆盖原有存储空间
G:将存储空间的内容追加到模式空间
g:将存储空间的内容复制到模式空间

浙公网安备 33010602011771号