sed 是一种行编辑器,一次处理一行内容,处理时把当前处理的行存储在一个临时缓冲区,称为“模式空间”(pattern space),接着用sed命令处理模式空间中的内容,处理完成后把模式空间中的内容打印到屏幕上,完成一行的编辑。 注:默认情况下sed并不做原地修改,不会修改源文件sed有两种使用方法 sed [options] command file(s) 直接编写处理命令处理 sed [options] -f scriptfile file(s) 使用预先编写好的sed脚本处理 options: -n 静默模式,不输出内容到屏幕 -e 多点编辑,一次编辑运行多个编辑命令 -f 指定预先写好的sed'脚本 -r 使用扩展的正则表达式 -i 修改源文件 地址定界 1、不给地址:对全文进行处理 2、单地址: #:第#行 /pattrern/:被此处模式匹配到的每一行 3、地址范围: #,# #,+# /pat1/,/pat2/ #,/pat1/ 4、~ 步进 sed -n '1~2p' file 显示基数行 sed -n '2~2p' file 显示偶数行
[root@makeISO mage]# cat 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed -n -e '1p' -e '3,4p' -e '6,+1p' 991 X 1 = 1 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 [root@makeISO mage]# sed -n -e '1p' -e '3,4p' -e '6,+1p' -e '/^9/p' 991 X 1 = 1 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed -i -n -e '1p' -e '3,4p' -e '6,+1p' -e '/^9/p' 99[root@makeISO mage]# cat 991 X 1 = 1 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed -n '1~2p' 991 X 1 = 1 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed -n '2~2p' 992 X 1 = 2 2 X 2 = 4 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64
编辑命令: d: 删除 cat -n /etc/passwd |sed '1,8d' 删掉第1-8行 sed '/^$/d' /etc/fstab 删掉空行 sed '/^#/d' /etc/fstab 删掉#开头的行
[root@makeISO mage]# sed '3,9d' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 [root@makeISO mage]# sed -e '/^1/d' -e '/^3/d' -e '/^5/d' -e '/^7/d' -e '/^9/d' 992 X 1 = 2 2 X 2 = 4 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64
p: 显示模式空间中的内容 cat -n /etc/passwd |sed '1,8p' 显示全文加匹配到的行 cat -n /etc/passwd |sed -n '1,8p' 仅显示匹配到的行 sed -n '#p' 与 sed '#d' 显示结果正好相反
[root@makeISO mage]# sed -n '1,5p' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 [root@makeISO mage]# sed '1,5d' 996 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81
a: \text 在指定行后面追加一行或多行文本(\n) sed -n '/^UUID/a \new line 1' /etc/fstab sed -n '/^UUID/a \new line 1\nnew line 2' /etc/fstab
[root@makeISO mage]# sed '/^5/a newline1' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 newline16 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed '/^5/a newline1\nnewline2' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 newline1newline26 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81
i: \text 在指定行前面插入一行或多行文本(\n) sed -n '/^UUID/i \new line 1' /etc/fstab sed -n '/^UUID/i \new line 1\nnew line 2' /etc/fstab
[root@makeISO mage]# sed '/^5/i newline1' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 newline15 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed '/^5/i newline1\nnewline2' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 newline1newline25 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81
c: \text 替换指定行尾一行或多行文本(\n) sed -n '/^UUID/c \new line 1' /etc/fstab sed -n '/^UUID/c \new line 1\nnew line 2' /etc/fstab
[root@makeISO mage]# sed '/^5/c newline1' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 newline16 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed '/^5/c newline1\nnewline2' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 newline1newline26 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81
w: 保存模式空间中内容到指定文件 sed -n '/^UUID/w file' /etc/fstab
[root@makeISO mage]# sed -n '2~2p' 992 X 1 = 2 2 X 2 = 4 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 [root@makeISO mage]# sed -n '2~2w file' 99[root@makeISO mage]# cat file 2 X 1 = 2 2 X 2 = 4 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64
=: 显示模式空间中行的行号 sed '/^UUID/=' /etc/fstab
[root@makeISO mage]# sed -n '/^5/=' 995
r: 读取文件到符合匹配条件的每一行后 sed '6r file' /etc/fstab 读取file内容插入到fstab文件第6行后面
[root@makeISO mage]# cat file aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[root@makeISO mage]# sed '^6r file' 99sed: -e expression #1, char 1: unknown command: `^'[root@makeISO mage]# sed '6r file' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed '/^6/r file' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81
!: 对地址定界取反 cat -n /etc/passwd |sed '1,8!d' 删掉第1-8行以外的行
[root@makeISO mage]# sed '1~2d' 992 X 1 = 2 2 X 2 = 4 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 [root@makeISO mage]# sed '1~2!d' 991 X 1 = 1 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81
s///: 替换,支持其他分隔符 s@@@ s### 替换标记: g: 行内全局替换 i: 忽略字符大小写
[root@localhost ~]# sed -n 's@r..t@&+++++++++++++@gp' /etc/passwdroot+++++++++++++:x:0:0:root+++++++++++++:/root+++++++++++++:/bin/bashoperator:x:11:0:operator:/root+++++++++++++:/sbin/nologinftp:x:14:50:FTP User:/var/ft+++++++++++++p:/sbin/nologin
[root@makeISO mage]# sed 's/X/*/' 991 * 1 = 1 2 * 1 = 2 2 X 2 = 4 3 * 1 = 3 3 X 2 = 6 3 X 3 = 9 4 * 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 * 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 * 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 * 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 * 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 * 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed 's/X/*/g' 991 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 [root@makeISO mage]# sed 's/x/*/g' 991 X 1 = 1 2 X 1 = 2 2 X 2 = 4 3 X 1 = 3 3 X 2 = 6 3 X 3 = 9 4 X 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 X 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 X 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 X 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 [root@makeISO mage]# sed 's/x/*/gi' 991 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 [root@makeISO mage]# sed 's/x/*/i' 991 * 1 = 1 2 * 1 = 2 2 X 2 = 4 3 * 1 = 3 3 X 2 = 6 3 X 3 = 9 4 * 1 = 4 4 X 2 = 8 4 X 3 = 12 4 X 4 = 16 5 * 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 6 * 1 = 6 6 X 2 = 12 6 X 3 = 18 6 X 4 = 24 6 X 5 = 30 6 X 6 = 36 7 * 1 = 7 7 X 2 = 14 7 X 3 = 21 7 X 4 = 28 7 X 5 = 35 7 X 6 = 42 7 X 7 = 49 8 * 1 = 8 8 X 2 = 16 8 X 3 = 24 8 X 4 = 32 8 X 5 = 40 8 X 6 = 48 8 X 7 = 56 8 X 8 = 64 9 * 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81
高级命令 h: 把模式空间内容覆盖到保持空间 H: 把模式空间中的内容追加到保持空间 g: 从保持空间中取回内容覆盖到模式空间 G: 从保持空间中取回内容追加到模式空间 x: 包模式空间和保持空间的内容互换 n: 读取匹配到行的下一行覆盖至模式空间 N: 读取匹配到行的下一行追加至模式空间 d: 删除模式空间中的行 D: 删除多行模式空间中的所有行 sed -n 'n;p' file 显示偶数行
[root@localhost ~]# cat -n /etc/passwd | sed -n 'n;p'2 bin:x:1:1:bin:/bin:/sbin/nologin4 adm:x:3:4:adm:/var/adm:/sbin/nologin6 sync:x:5:0:sync:/sbin:/bin/sync8 halt:x:7:0:halt:/sbin:/sbin/halt10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
sed '1!G;h;$!d' file 逆向显示文件内容
[root@localhost ~]# cat qq123456[root@localhost ~]# cat qq | sed '1!G;h;$!d'654321
sed '$!N;$!D' file 取文件后两行
[root@localhost ~]# cat qq123456[root@localhost ~]# cat qq | sed '$!N;$!D'56
sed '$!d' file 取文件最后一行
[root@localhost ~]# sed '$!d' qq6[root@localhost ~]#
sed 'G' file 每行下面添加空白行
[root@localhost ~]# cat qq | sed 'G'123456[root@localhost ~]#
sed '/^$/d;G' file 删除已有空行,然后给每行添加空行
[root@localhost ~]# cat qq 123456[root@localhost ~]# sed '/^$/d;G' qq123456[root@localhost ~]#
sed 'n;d' file 取基数行
[root@localhost ~]# sed 'n;d' qq135[root@localhost ~]#
sed -n '1!G;h;$p' file 逆序显示文件内容
[root@localhost ~]# sed -n '1!G;h;$p' qq654321[root@localhost ~]#
练习: 1、删除某文件中以空白开头的行的行首的空白字符
[root@localhost ~]# cat qq 1 2 3 4 5 6[root@localhost ~]# sed 's/^[[:space:]]\+//' qq123456[root@localhost ~]#
2、删除某文件中所有以# 开头,后面至少跟一个空白字符的行行首的#和空白字符
[root@localhost ~]# cat qq# 1# 2#3# 4# 5# 6[root@localhost ~]# sed 's/^#[[:space:]]\+//' qq12#3456[root@localhost ~]#
3、echo一个绝对路径,分别取其基名和目录名
[root@localhost ~]# echo /etc/sysconfig/network-scripts/ifcfg-lo/ |sed 's@[^/]\+/\?$@@'/etc/sysconfig/network-scripts/[root@localhost ~]# echo /etc/sysconfig/network-scripts/ifcfg-lo |sed 's@[^/]\+/\?$@@'/etc/sysconfig/network-scripts/[root@localhost ~]# echo /etc/sysconfig/network-scripts/ifcfg-lo |sed -e 's@/$@@' -e 's@.*/@@'ifcfg-lo[root@localhost ~]# echo /etc/sysconfig/network-scripts/ifcfg-lo/ |sed -e 's@/$@@' -e 's@.*/@@'ifcfg-lo