一 . Sed 工具概述
1.1 sed 概述
Stream EDitor 流式编辑器
-非交互 基于模式匹配过滤及修改文本
-逐行处理 并将结果输出到屏幕
-可实现对文本的输出 删除 替换 复制 剪切 导入 导出等各种操作
1.2 sed 命令格式
-格式1 前置命令 | sed [选项] ‘编辑命令’
-格式2 sed [选项] ‘条件命令’ 文件…(非交互式)
二 . Sed 基本选项
2.1 p : 打印
[root@shell ~]# sed -n ‘3,5p’ /etc/passwd #同时打印第3-5行的内容
[root@shell ~]# sed -n ‘3p;5p’ /etc/passwd #同时打印第3和第2行内容
[root@shell ~]# sed ‘3,+10p’ /etc/passwd #打印第3行以及后面的10行
[root@shell ~]# sed -n ‘1~2p’ /etc/passwd #打印奇数行
[root@shell ~]# sed -n ‘2~2p’ /etc/passwd #打印偶数行
[root@shell ~]# sed ‘2p’ /etc/hosts #指定第2行打印2遍
结合正则表达式使用
[root@shell ~]# sed -n ‘/root/p’ /etc/passwd #打印包含 root 的所有行
[root@shell ~]# sed -n ‘/^root/p’ /etc/passwd #打印以 root 开头的所有行
[root@shell ~]# sed -n ‘/bash$/p’ /etc/passwd #打印bash结尾的行
2.2 d : 删除
[root@shell ~]# cp -r /etc/passwd /dan.txt #生成一个自定义文件
[root@shell ~]# sed ‘1,11d’ /dan.tx