目录
(2)替换处\1\...\n\ 对应需要替换内容处每个括号中子表达式(可以是字符串和正则)
sed是一种流编辑器,流编辑器会在编辑器处理数据前基于预先提供的一组规则来编辑数据。
sed编辑器可以根据命令来处理数据流中的数据,通过多种转换修改流经它的文本,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。
sed是一个面向字符流的非交互式编辑器,也就是说 sed 不允许用户与它进行交互操作。
sed从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的内存缓冲区中(又称模式空间,pattern space)。
默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。
发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
注:默认情况下所有的sed命令都是在模式空间内执行的,不会影响到文件中的内容,除非是用重定向输出到文件或使用-i选项。
sed [选项] 操作 文件名...
常用选项
| 选项 | 作用 |
|---|---|
| -e | 表示用指定命令来处理输入的文本文件,一般在执行多个操作命令使用(只有一个操作命令时可省略)。 |
| -f | 表示用指定的脚本文件来处理输入的文本文件。 |
| -n | 禁止sed编辑自动输出,但可以与p命令一起使用完成输出。 |
| -i | 直接修改目标文本文件。 |
常用操作
| 操作 | 作用 |
|---|---|
| a | 插入,在当前行下面增加一行指定内容。 |
| i | 插入,在选定行上面插入一行指定内容。 |
| d | 删除,删除指定的行。 |
| s | 替换,替换选定的字符。 |
| c | 替换,将选定行替换为指定内容。 |
| p | 打印,如果同时指定行,表示打印指定行,如果不指定行,则表示打印所有内容,如果有非打印字符,则以ASCII码输出。其通常与"-n"选项一起使用。 |
| = | 打印行号。 |
| l | 打印数据流中的文本和不可打印的ASCII码字符(比如结束符$、制表符\t)。 |
执行多个操作的方法
- sed -e '操作1' -e '操作2' ... 文件名
- sed -e '操作1;操作2;...' 文件名
sed本身有执行操作后自动打印的效果,-n禁用,p操作也可以打印,并且可以限定条件
sed ' ' 文件名
sed -n 'p' 文件名
- //使用sed自动打印
- [root@localhost1 sedtest]#sed '' file1
- one
- two
- three
- four
- five
- six
- seven
- eight
- nine
- ten
-
- //使用sed的p操作打印
- [root@localhost1 sedtest]#sed -n 'p' file1
- one
- two
- three
- four
- five
- six
- seven
- eight
- nine
- ten
sed -n '指定条件p' 文件名
sed -n '/字符串或正则表达式/p' 文件名
- //1.打印指定行(根据行号)
- [root@localhost1 sedtest]#sed -n '2p' file1 //打印第2行
- two
-
- //2.打印指定的多行(根据行号)
- [root@localhost1 sedtest]#sed -n '7,$p' file1 //打印从第7行到末尾行
- seven
- eight
- nine
- ten
-
- //3.打印多行后退出
- [root@localhost1 sedtest]#sed '3q' file1 //从头打印3行后退出
- one
- two
- three
-
- //4.打印指定行(根据字符串)
- [root@localhost1 sedtest]#sed -n '/ve/p' file1 //根据字符串s
- five
- seven
-
- //5.从指定行打印到包含指定字符串的行(根据字符串)
- [root@localhost1 sedtest]#sed -n '7,/nobody/p' /etc/passwd //打印7到包含nobody的行
- 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
-
-
- //6.打印奇偶行
- [root@localhost1 sedtest]#sed -n 'n;p' file1 //n表示跳到下一行,先n实现输出偶数行
- two
- four
- six
- eight
- ten
- [root@localhost1 sedtest]#sed -n 'p;n' file1 //后n实现奇数行
- one
- three
- five
- seven
- nine
-
- //7.正则表达式表示
- //根据root开头或包含mail的输出行(包含扩展正则时sed一定需要加-r选项)
- [root@localhost1 sedtest]#sed -nr '/^root|mail/p' /etc/passwd
- root:x:0:0:root:/root:/bin/bash
- mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
-
- //从第8行打印到包含(r 至少有一个a t)的字符串的行
- [root@localhost1 sedtest]#sed -nr '8,/ra{1,}t/p' /etc/passwd
- 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
-
sed '指定行范围a 指定内容' 文件名
- //指定第2行下增加行内容
- [root@localhost1 ~]#sed '2a ABC' file1
- One Two Three
- Four Five Six
- ABC
- Seven Eight Nine
- Ten Eleven Twelve
-
- //指定2到4行每行下增加行内容
- [root@localhost1 ~]#sed '2,4a ABC' file1
- One Two Three
- Four Five Six
- ABC
- Seven Eight Nine
- ABC
- Ten Eleven Twelve
- ABC
-
sed '指定行范围i 指定内容' 文件名
- //指定第2行上插入行内容
- [root@localhost1 ~]#sed '2i ABC' file1
- One Two Three
- ABC
- Four Five Six
- Seven Eight Nine
- Ten Eleven Twelve
-
- //指定第1到3行上插入行内容
- [root@localhost1 ~]#sed '1,3i ABC' file1
- ABC
- One Two Three
- ABC
- Four Five Six
- ABC
- Seven Eight Nine
- Ten Eleven Twelve
sed '指定行范围c 指定内容' 文件名
- //指定第3行替换为指定行内容
- [root@localhost1 ~]#sed '3c ABC' file1
- One Two Three
- Four Five Six
- ABC
- Ten Eleven Twelve
-
- //指定第3到4行替换为指定行内容(多行变为一行)
- [root@localhost1 ~]#sed '3,4c ABC' file1
- One Two Three
- Four Five Six
- ABC
-
- //指定包含ve的行替换为指定行内容
- [root@localhost1 ~]#sed '/ve/c ABC' file1
- One Two Three
- ABC
- ABC
- ABC
sed '指定条件d' 文件名
sed '/字符串或正则表达式/d' 文件名
- //1.删除所有行(根据行号)
- [root@localhost1 sedtest]#sed 'd' file1
-
- //2.删除指定行(根据行号)
- [root@localhost1 sedtest]#sed '1d' file1 //删除第1行
- two
- three
- four
- five
- six
- seven
- eight
- nine
- ten
-
- //3.删除指定的多行(根据行号)
- [root@localhost1 sedtest]#sed '2,8d' file1 //删除2到8行
- one
- nine
- ten
-
- //4.删除空行
- [root@localhost1 sedtest]#sed '/^$/d' /etc/fstab
- #
- # /etc/fstab
- # Created by anaconda on Fri Jul 29 21:42:19 2022
- #
- # Accessible filesystems, by reference, are maintained under '/dev/disk'
- # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
- #
- /dev/mapper/centos-root / xfs defaults 0 0
- UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
- /dev/mapper/centos-swap swap swap defaults 0 0
-
- //5.删除指定的多行(根据字符串)
- [root@localhost1 sedtest]#sed '/ve/d' file1 //删除包含ve的行
- one
- two
- three
- four
- six
- eight
- nine
- ten
-
- //6.正则表达式表示
- //删除结尾为bash或nologin的行
- [root@localhost1 sedtest]#sed -r '/bash$|nologin$/d' /etc/passwd
- sync:x:5:0:sync:/sbin:/bin/sync
- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
- halt:x:7:0:halt:/sbin:/sbin/halt
-
- //7.从包含第1个字符串的行开始删除,到包含第2个字符串的位置停止删除
- //(如果找不到第2个字符串的行,则删到最后一行)
- [root@localhost1 sedtest]#sed '/one/,/five/d' file1 //从含one的行删到含five的行
- six
- seven
- eight
- nine
- ten
-
- [root@localhost1 sedtest]#sed '/thr/,/no/d' file1 //从含thr的行开始删,匹配不到含no的行,则删到最后
- one
- two
-
-
- //8.反向删除(删除除了包含指定内容的行)
- [root@localhost1 sedtest]#sed '/ve/d' file1 //删除除包含ve的行
- five
- seven
-
sed [选项] '行范围 s/旧字符串/新字符串/替换标记' 文件名
4种替换标记
| 替换标记 | 作用 |
|---|---|
| 数字n | 表明新字符串将替换第n处匹配的地方 |
| g | 表明新字符串将会替换所有匹配的地方 |
| p | 打印与替换命令匹配的行,需要在前面加 -n选项 |
| w | 将替换的结果写到文件中 |
- //1.不加范围,替换每行查找到的字符串
- [root@localhost1 ~]#sed -n 's/root/admin/p' /etc/passwd
- admin:x:0:0:root:/root:/bin/bash
- operator:x:11:0:operator:/admin:/sbin/nologin
-
- //2.指定范围行,在指定行中替换n次(默认1次)
- [root@localhost1 ~]#sed -n '/^root/ s/root/abc/p' /etc/passwd
- abc:x:0:0:root:/root:/bin/bash
- [root@localhost1 ~]#sed -n '/^root/ s/root/abc/2p' /etc/passwd
- root:x:0:0:abc:/root:/bin/bash
-
- //3.替换行中所有,使用g
- [root@localhost1 ~]#sed -n '/^root/ s/root/abc/gp' /etc/passwd
- root:x:0:0:root:/abc:/bin/bash
-
- //4.指定行s/^/#/可以实现注释行内容
- [root@localhost1 ~]#sed '2 s/^/#/' file1
- one two three
- #four five six
- seven eight nine
- ten eleven twelve
-
- //5.指定行范围替换
- [root@localhost1 ~]#sed '1,3 s/^/#/' file1
- #one two three
- #four five six
- #seven eight nine
- ten eleven twelve
-
- //指定替换除为空可以实现将指定字符串删除
- [root@localhost1 ~]#sed 's/#//' /etc/fstab
-
- /etc/fstab
- Created by anaconda on Fri Jul 29 21:42:19 2022
-
- Accessible filesystems, by reference, are maintained under '/dev/disk'
- See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
-
- /dev/mapper/centos-root / xfs defaults 0 0
- UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
- /dev/mapper/centos-swap swap swap defaults 0 0
- //编写文件包含sed的操作
- [root@localhost1 ~]#vim script.txt
- /^$/d
- /e$/ s/^/#/
- 1,3 s/#//
-
- //测试用文件
- [root@localhost1 ~]#vim file2
- #one
- #two
- #three
- four
- five
-
-
- six
- seven
- eight
-
- nine
- ten
-
- //sed指定文件操作
- [root@localhost1 ~]#sed -f script.txt file2
- #one
- two
- #three
- four
- five
- six
- seven
- eight
- #nine
- ten
- //加-i直接对文件进行操作
- [root@localhost1 ~]#sed -i '/swap/ s/^/#/' /etc/fstab
-
- //注释文件中包含swap的行
- [root@localhost1 ~]#vim /etc/fstab
- # /etc/fstab
- # Created by anaconda on Fri Jul 29 21:42:19 2022
- #
- # Accessible filesystems, by reference, are maintained under '/dev/disk'
- # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
- #
- /dev/mapper/centos-root / xfs defaults 0 0
- UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
- #/dev/mapper/centos-swap swap swap defaults 0 0
sed [选项] '行范围 s/正则表达式/&/替换标记' 文件名
- //这里的&代表.*swap.*匹配到的内容(即包含swap的行内容),在前面加上#可以实现注释行
- [root@localhost1 ~]#sed -n 's/.*swap.*/#&/' /etc/fstab
- #/dev/mapper/centos-swap swap swap defaults 0 0
sed [选项] 's/(表达式1)..(表达式n)/\1\2\...\n/' 文件名
- //原文件
- [root@localhost1 ~]#cat file3
- 123456abcdef
-
- //字符串子表达示,"\数字" 一一对应
- [root@localhost1 ~]#sed 's/(123)(456)(abc)/\3\2\1/' file3
- abc456123def
-
- //正则子表达示,"\数字" 一一对应
- [root@localhost1 ~]#sed -r 's/([0-9]{6})([a-z]{6})/\2\1/' file3
- abcdef123456
sed 's/[a-z]/\u&/g' 文件名 (原字符范围前\b实现首字母转大写)
sed 's/[A-Z]/\l&/g' 文件名
- //原文件
- [root@localhost1 ~]#cat file1
- one two three
- four five six
- seven eight nine
- ten eleven twelve
-
- //1.全部转为大写
- [root@localhost1 ~]#sed 's/[a-z]/\u&/g' file1
- ONE TWO THREE
- FOUR FIVE SIX
- SEVEN EIGHT NINE
- TEN ELEVEN TWELVE
-
- [root@localhost1 ~]#sed -i 's/[a-z]/\u&/g' file1
-
- //2.全部转为小写
- [root@localhost1 ~]#sed 's/[A-Z]/\l&/g' file1
- one two three
- four five six
- seven eight nine
- ten eleven twelve
-
- //3.锚定首字母,实现首字母大小写转换
- [root@localhost1 ~]#sed 's/\b[a-z]/\u&/g' file1
- One Two Three
- Four Five Six
- Seven Eight Nine
- Ten Eleven Twelve
/U/对应子表达式位置数/E (结合第2条操作实现部分转大小写)
- //部分转大小写,实现修改文件前缀名小写转大写
- [root@localhost1 ~]#ls *.txt | sed -r 's/(.+)(\.txt)/\u\1\E\2/' //u转开头
- Abc.txt
- A.txt
- B.txt
- Efg.txt
- Number.txt
- Script.txt
-
- [root@localhost1 ~]#ls *.txt | sed -r 's/(.+)(\.txt)/\U\1\E\2/' //U转全部
- ABC.txt
- A.txt
- B.txt
- EFG.txt
- NUMBER.txt
- SCRIPT.txt
sed -i '$r 文件1' 文件2
- //往fa、fb文件输入内容
- [root@localhost1 ~]#echo 12345678 > fa
- [root@localhost1 ~]#echo abcdefgh > fb
-
- //合并文件内容到fb文件中
- [root@localhost1 ~]#sed -i '$r fa' fb
- [root@localhost1 ~]#cat fb
- abcdefgh
- 12345678
H 复制 ; G 粘贴
- //原文件
- [root@localhost1 ~]#cat file2
- zhang san
- li si
- wang wu
- zhao liu
- qian qi
- zhu ba
-
- //1.复制粘贴到末尾
- [root@localhost1 ~]#sed '1,2H;$G' file2
- zhang san
- li si
- wang wu
- zhao liu
- qian qi
- zhu ba
-
- zhang san
- li si
-
- //2.剪切粘贴到末尾
- [root@localhost1 ~]#sed '1,2{H;d};$G' file2
- wang wu
- zhao liu
- qian qi
- zhu ba
-
- zhang san
- li si