Sed 语法和基本命令 打印、删除、和写入(1)

Sed 代表 Strem Editor(流编辑器),是操作、过滤和转换文本内容的强大工具。Sed 可以从文件和管道中读取输入。在你的 bash 启动文件中,就可能有不少用来设置各种环境的 sed 命令。

1.它打印出/etc/passwd 文件中的所有行,使用p打印时,必须和-n参数一起用,否则会打印两遍
sed –n ‘p’ /etc/passwd

[root@localhost /]# sed 'p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@localhost ~]# sed -n 'p' /etc/passwd
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

2.把多个 sed 命令合并到一个文件中,这个文件被称为 sed 脚本,然后使用-f 选项调用它
打印/etc/passwd 问中以 root 和 nobody0开头的行:
(使用调用的方法)
先编辑一个脚本文件
vi test-script.sed
/^root/ p
/^nobody/ p
sed –n –f test-script.sed /etc/passwd

[root@localhost ~]# sed -n -f test-script.sed /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin

3.-e参数 的使用方法,它打印/etc/passwd 中以 root 和 nobody 开头的行
sed -n -e ‘/^root/p’ -e ‘/^nobody/p’ /etc/passwd

[root@localhost ~]# sed -n -e '/^root/p' -e '/^nobody/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:X:99:99:Nobody:/:/sbin/nologin

也可以果使用-e 执行多个命令,也可以使用反斜杠\把它们分割到多行执行
sed -n \ 进入模式空间

[root@localhost ~]# sed -n \
> -e '/^root/p' \
> -e '/^nobody/p' \
> /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin

4.指定地址范围
只打印第 2 行

[root@localhost /]# sed -n '2 p' employee.txt
102,Jason Smith,IT Manager

打印第 1 至第 4 行

[root@localhost /]# sed -n '1,4 p' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer

打印第 2 行至最后一行($代表最后一行

[root@localhost /]# sed -n '2,$ p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

5.修改地址范围
波浪号~也可以指定地址范围,它指定每次要跳过的行数。如 n~m 表示从第 n 行开始,每次
跳过 m 行。

只打印奇数行

[root@localhost /]# sed -n '1~2 p' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
105,Jane Miller,Sales Manager

6.模式匹配
打印匹配模式”Jane”的行

[root@localhost /]# sed -n '/Jane/p' employee.txt
105,Jane Miller,Sales Manager

打印第一次匹配 Jason 的行至第 4 行的内容

[root@localhost /]# sed -n '/Jason/,4 p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer

打印从第一次匹配 Raj 的行到最后的所有行

[root@localhost /]# sed -n '/Raj/,$ p' employee.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

打印匹配 Jason 的行和其后面的两行

[root@localhost /]# sed -n '/Jason/,+2 p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin

7.删除行
命令 d 用来删除行,需要注意的是它只删除模式空间的内容,和其他 sed 命令一样,命令 d不会修改原始文件的内容。

只删除第 2 行

[root@localhost /]# sed '2d' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

删除第 1 至第 4 行

[root@localhost /]# sed '2,4d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager

删除第 2 行至最后一行

[root@localhost /]# sed '2,$d' employee.txt
101,John Doe,CEO

只删除奇数行

[root@localhost /]# sed '1~2 d' employee.txt
102,Jason Smith,IT Manager
104,Anand Ram,Developer

删除从第一次匹配 Jason 的行至第 4 行

[root@localhost /]# sed '/Jason/,4 d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager

删除从第一次匹配 Raj 的行至最后一行

[root@localhost /]# sed '/Raj/,$ d' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager

删除第一次匹配 Jason 的行和紧跟着它后面的两行

[root@localhost /]# sed '/Jason/,+2 d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager

删除所有空行
sed ‘/^$/ d’ employee.txt
删除所有注释行(假定注释行以#开头)
sed ‘/^#/ ‘ employee.txt
注意:如果有多个命令,sed 遇到命令 d 时,会删除匹配到的整行数据,其余的命令将无法
操作被删除的行。

8.把模式空间内容写到文件中(w 命令)
把 employee.txt 的内容保存到文件 output.txt,同时显示在屏幕上

[root@localhost /]# sed 'w output.txt' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[root@localhost /]# cat o
opt/        output.txt
[root@localhost /]# cat output.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

把 employee.txt 的内容保存到文件 output.txt,但不在屏幕上显示

[root@localhost /]# rm -rf output.txt
[root@localhost /]# sed -n 'w output.txt' employee.txt
[root@localhost /]# cat output.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

只保存第 2 行

[root@localhost /]# rm -rf output.txt
[root@localhost /]# sed -n '2w output.txt' employee.txt
[root@localhost /]# cat output.txt
102,Jason Smith,IT Manager

保存第 2 行起至最后一行

[root@localhost /]# sed -n '2,$w output.txt' employee.txt
[root@localhost /]# cat output.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

保存匹配 Raj 的行至匹配 Jane 的行

[root@localhost /]# sed -n '/Raj/,/Jane/ w  output.txt' employee.txt
[root@localhost /]# cat output.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

保存匹配 Jason 的行以及紧跟在其后面的两行

[root@localhost /]# rm -rf output.txt
[root@localhost /]# sed -n '/Raj/,+2 w  output.txt' employee.txt
[root@localhost /]# cat output.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

猜你喜欢

转载自blog.csdn.net/weixin_43150761/article/details/83113009