【sed 流编辑器】
sed是一种非交互式的编辑器,它不会对源文件进行任何的修改。将源文件调入内存缓冲区,然后根据匹配模式进行相应的修改,将修改结果输出。但不影响源文件的内容。
sed的语法格式:
sed [-nefr] [动作]
选项:
-n :只显示匹配的行
-e :直接在命令列模式上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是输出到终端。
动作说明:m,n[动作]
m,n 一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』
动作:
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行),支持使用\n实现多行追加
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作!例如 1,20s/old/new/g 就是啦!
w /path/somefile :保存模式匹配的行至指定文件
r /path/somefile :读取指定文件的文本至模式空间中匹配到的行后
= :为模式空间中的行打印行号
! :模式空间中匹配行取反处理
地址定界:
(1) 不给地址:对全文进行处理
(2) 单地址:
m: 指定的行
/pattern/:被此处模式所能够匹配到的每一行
(3) 地址范围
m,n:从第m行开始到第n行结束
m,+n:从第m行开始,到 m+n 行结束
m,/pat1/:从第m行开始,到第一次被模式匹配的行结束
/pat1/,/pat2/:从被模式1匹配到的第一行开始,到被模式2匹配到的第一行结束;
$:最后一行
(4)步进:~
1~2:表示从1开始,步进2行,如3,5,7行即所有奇数行
2~2:表示所有偶数行
sed处理文本,增删查改
root@bras-01:/tmp# cat -n test.txt
1 # This is a test
2 # A multi-tier architecture for both Web services and
3 # dynamic Web applications leads to reusability, simplicity,
4 # extensibility, and clear separation of component responsibilities.
5
6 # www.baidu.com
7 # www.12335.com
8 # www.taobao.com
9
10 # 192.168.1.1
11 # 192.168.1.254
12
13 #!/bin/bash
14
15 # 试一试
16 # 菜单
17
18 echo "欢迎来到虚空台,凡人,这里你可以得到匹敌世界的财富,拥有灭星的力量,只要...你能活下去"
19 function mainmenu() {
20 echo ' =========================================== '
21 echo ' ** 1.虚空斗场 ** '
22 echo ' ** 2.落日神殿 ** '
23 echo ' ** 3.殷红梦境 ** '
24 echo ' ** 4.离开此界 ** '
25 echo " =========================================== "
26 read -n 1 -p "选择吧,你要传送去的地方: " option
27 echo -e "\n"
28 # options=( 虚空斗场 落日神殿 殷红梦境 离开此界 )
29 case $option in
30 1)
31 echo -e "\n骗你的,二狗子,欢迎来到王者荣耀,敌军还有5秒到达战场,全军出击,冲》》》》\n"
32 mainmenu
33 ;;
34 2)
35 echo -e "\n欢迎...呃,你被典韦捅死了,下辈子注意一点...,感谢你的体验,拜拜\n"
36 mainmenu
37 ;;
38 3)
39 echo -e "\n庄生晓梦迷蝴蝶,只缘身在此山中...\n"
40 mainmenu
41 ;;
42 4)
43 echo -e "\n猪八戒一个屁,把你崩了出去....\n"
44 exit
45 ;;
46 *)
47 echo "输入错误,请从新输入"
48 mainmenu
49 ;;
50 esac
51 }
52 mainmenu
53
54 # lallalalalla
55 # qiu shan
# 提取10-13行的内容
root@bras-01:/tmp# sed -n '10,13p' test.txt
# 去掉6-8行的注释
root@bras-01:/tmp# sed -i '6,8s/^#//' test.txt
# 6-8注释掉
root@bras-01:/tmp# sed -i '6,8s/^/#/' test.txt
# 6-8行尾添加字符串test
root@bras-01:/tmp# sed -i '6,8s/$/ test/' test.txt
# 提取以 test 结尾的行
root@bras-01:/tmp# sed -n '/test$/p' test.txt
# 显示以 test 结尾的行开始到 #! 开头的行结束的所有行
root@bras-01:/tmp# sed -n '/test$/,/^#!/p' test.txt
# 显示奇数行,即从第一行开始,每隔两行显示一行
root@bras-01:/tmp# sed -n '1~2p' test.txt
# 删除上面 test.xt 的 17-53行
root@bras-01:/tmp# sed -i '17,53d' test.txt
root@bras-01:/tmp# cat -n test.txt
1 # This is a test
2 # A multi-tier architecture for both Web services and
3 # dynamic Web applications leads to reusability, simplicity,
4 # extensibility, and clear separation of component responsibilities.
5
6 # www.baidu.com
7 # www.12335.com
8 # www.taobao.com
9
10 # 192.168.1.1
11 # 192.168.1.254
12
13 #!/bin/bash
14
15 # 试一试
16 # 菜单
17 # lallalalalla
18 # qiu shan
# 过滤出 /dev/sd 开头的行
root@bras-01:/# df | sed -n '/^\/dev\/sd/p'
/dev/sda1 20510332 7972624 11472800 42% /
# 在2-4行后面分别追加自定义字符,斜线\表示之后出现的所有字符都算内容
root@bras-01:/tmp# sed '2,4a\ ****************** \n ******************' test.txt
# This is a test
# A multi-tier architecture for both Web services and
******************
******************
# dynamic Web applications leads to reusability, simplicity,
******************
******************
# extensibility, and clear separation of component responsibilities.
******************
******************
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
#!/bin/bash
# 试一试
# 菜单
# lallalalalla
# qiu shan
# 在2-4行前面分别追加自定义字符,斜线\表示之后出现的所有字符都算内容
root@bras-01:/tmp# sed '2,4i\ ================ \n ================ ' test.txt
# This is a test
================
================
# A multi-tier architecture for both Web services and
================
================
# dynamic Web applications leads to reusability, simplicity,
================
================
# extensibility, and clear separation of component responsibilities.
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
#!/bin/bash
# 试一试
# 菜单
# lallalalalla
# qiu shan
# 把以 #! 开头的行替换为自定义字符串
root@bras-01:/tmp# sed '/^#!/c\ 6666666' test.txt
# This is a test
# A multi-tier architecture for both Web services and
# dynamic Web applications leads to reusability, simplicity,
# extensibility, and clear separation of component responsibilities.
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
6666666
# 试一试
# 菜单
# lallalalalla
# qiu shan
# 将 test.txt 中以 # 开头的行保存到 /tmp/haha.txtzhong
root@bras-01:/tmp# sed -n '/^#/w /tmp/haha.txt' test.txt
root@bras-01:/tmp# cat haha.txt
# This is a test
# A multi-tier architecture for both Web services and
# dynamic Web applications leads to reusability, simplicity,
# extensibility, and clear separation of component responsibilities.
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
#!/bin/bash
#! haha
# 试一试
# 菜单
# lallalalalla
# qiu shan
# 读取 /tmp/haha.txt 中的内容并将其插入到 test.txt 文档第19行之后
root@bras-01:/tmp# sed '19r /tmp/haha.txt' test.txt
# This is a test
# A multi-tier architecture for both Web services and
# dynamic Web applications leads to reusability, simplicity,
# extensibility, and clear separation of component responsibilities.
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
#!/bin/bash
#! haha
# 试一试
# 菜单
# lallalalalla
# qiu shan
# This is a test
# A multi-tier architecture for both Web services and
# dynamic Web applications leads to reusability, simplicity,
# extensibility, and clear separation of component responsibilities.
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
#!/bin/bash
#! haha
# 试一试
# 菜单
# lallalalalla
# qiu shan
# 在匹配到的行之上添加行号
root@bras-01:/tmp# sed '/^#!/=' test.txt
# This is a test
# A multi-tier architecture for both Web services and
# dynamic Web applications leads to reusability, simplicity,
# extensibility, and clear separation of component responsibilities.
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
13
#!/bin/bash
14
#! haha
# 试一试
# 菜单
# lallalalalla
# qiu shan
# 显示没有被匹配到的行(结果取反)
root@bras-01:/tmp# sed -n '/^#!/!p' test.txt
# This is a test
# A multi-tier architecture for both Web services and
# dynamic Web applications leads to reusability, simplicity,
# extensibility, and clear separation of component responsibilities.
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
# 试一试
# 菜单
# lallalalalla
# qiu shan
===查找替换===
s sed命令会将s之后出现的字符视为命令分隔符,这允许我们更改默认的分隔符 /,如 s@@@,s|||
如果作为分隔符的字符出现在模式中,必须使用 \ 对其进行转义
替换标记:
g: 全局替换
p: 显示替换成功的行
w /PATH/TO/SOMEFILE:将替换成功的行保存至文件中
# 只替换了每行中模式首次匹配的内容
root@bras-01:/tmp# echo -e 'thisthisthisthis\nthisthisthisthis' | sed 's|this|THIS|'
THISthisthisthis
THISthisthisthis
# g 标记可以使sed执行全局替换
root@bras-01:/tmp# echo -e 'thisthisthisthis\nthisthisthisthis' | sed 's|this|THIS|g'
THISTHISTHISTHIS
THISTHISTHISTHIS
# ng 标记可以使sed从第n次开始替换
root@bras-01:/tmp# echo -e 'thisthisthisthis\nthisthisthisthis' | sed 's|this|THIS|2g'
thisTHISTHISTHIS
thisTHISTHISTHIS
root@bras-01:/tmp# echo -e 'thisthisthisthis\nthisthisthisthis' | sed 's|this|THIS|3g'
thisthisTHISTHIS
thisthisTHISTHIS
# 去掉每行开头的 #
root@bras-01:/tmp# sed -rn 's/^#+//p' test.txt
This is a test
A multi-tier architecture for both Web services and
dynamic Web applications leads to reusability, simplicity,
extensibility, and clear separation of component responsibilities.
www.baidu.com
www.12335.com
www.taobao.com
192.168.1.1
192.168.1.254
!/bin/bash
! haha
试一试
菜单
lallalalalla
qiu shan
# 在每一行开头添加 #
root@bras-01:/tmp# sed -rn 's/^/#/p' test.txt
# This is a test
# A multi-tier architecture for both Web services and
# dynamic Web applications leads to reusability, simplicity,
# extensibility, and clear separation of component responsibilities.
# www.baidu.com
# www.12335.com
# www.taobao.com
# 192.168.1.1
# 192.168.1.254
#!/bin/bash
#! haha
# 试一试
# 菜单
# lallalalalla
# qiu shan
# 提取 ipv4地址 广播地址 掩码
root@bras-01:/tmp# ifconfig ens3
ens3 Link encap:Ethernet HWaddr 52:54:00:29:42:f9
inet addr:212.2.1.221 Bcast:212.2.255.255 Mask:255.255.0.0
inet6 addr: fe80::b154:2723:cd20:2ab1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:109104656 errors:0 dropped:115489 overruns:0 frame:0
TX packets:25332 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:11410664519 (11.4 GB) TX bytes:2863263 (2.8 MB)
root@bras-01:/tmp# ifconfig ens3 | sed -rn 's/.*(inet addr:.*)[[:space:]](Bcast:.*)[[:space:]](Mask:.*)/\1\n\2\n\3/p'
inet addr:212.2.1.221
Bcast:212.2.255.255
Mask:255.255.0.0
========== 其他 ==========
1. \b表示单词的边界
2. 建议先使用不带 -i 选项的sed命令试一遍,确认OK不OK,然后再加入 -i 将更改写入文件
3. sed表达式通常使用单引号来引用,不过也可以使用双引号(模式中含有变量时可以派上用场)