一、sed介绍
二、sed的工作流程
三、sed命令的常见用法
sed[选项] ‘操作‘ 参数
sed [选项] -f 脚本文件 参数
3.1、sed命令选项
选项 | 解释 |
-e或-expression= | 表示用指定命令或者脚本来处理输入的文本文件 |
-f 或-file= | 表示用指定的脚本文件来处理输入的文本文件 |
-h或一help | 显示帮助 |
-n、-quiet 或silent | 表示仅显示处理后的结果 |
-i | 直接编辑文本文件,直接编辑源文件 |
3.2、操作命令
操作 | 解释 |
a | 增加,在当前行下面增加一行指定内容 |
c | 替换,将选定行替换为指定内容 |
d | 删除,删除选定的行 |
i | 插入,在选定行上面插入一行指定内容 |
p | 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。其通常与“-n”选项一起使用 |
s | 替换,替换指定字符 |
y | 字符转换 |
3.3.1、p - 输出符合条件的文本
[root@localhost ~]# sed -n ‘p‘ test.txt ‘输出所有内容,等同于 cat test.txt‘
...省略内容
[root@localhost ~]# sed -n ‘3p‘ test.txt ‘输出第 3 行‘
...省略内容
[root@localhost ~]# sed -n ‘3,5p‘ test.txt ‘输出第 3~5 行‘
...省略内容
[root@localhost ~]# sed -n ‘p;n‘ test.txt ‘输出所有奇数行,n 表示读入下一行资料‘
...省略内容
[root@localhost ~]# sed -n ‘n;p‘ test.txt ‘输出所有偶数行,n 表示读入下一行资料‘
...省略内容
[root@localhost ~]# sed -n ‘1,5{p;n}‘ test.txt ‘输出第 1~5 行之间的奇数行(第 1、3、5 行)‘
...省略内容
[root@localhost ~]# sed -n ‘10,${n;p}‘ test.txt ‘输出第 10 行至文件尾之间的偶数行‘
...省略内容
sed 命令与正则表达式结合使用
[root@localhost ~]# sed -n ‘/the/p‘ test.txt ‘输出包含the 的行‘
...省略部分内容
[root@localhost ~]# sed -n ‘4,/the/p‘ test.txt ‘输出从第 4 行至第一个包含 the 的行‘
...省略部分内容
[root@localhost ~]# sed -n ‘/the/=‘ test.txt ‘输出包含the 的行所在的行号,等号(=)用来输出行号‘
...省略部分内容
[root@localhost ~]# sed -n ‘/^PI/p‘ test.txt ‘输出以PI 开头的行‘
...省略部分内容
[root@localhost ~]# sed -n ‘/[0-9]$/p‘ test.txt ‘输出以数字结尾的行‘
...省略部分内容
[root@localhost ~]# sed -n ‘/\<wood\>/p‘ test.txt ‘输出包含单词wood 的行,\<、\>代表单词边界‘
...省略部分内容
3.3.2、d - 删除符合条件的文本
下面命令中 nl 命令用于计算文件的行数,结合该命令可以更加直观地查看到命令执行的结果
[root@localhost ~]# nl test.txt | sed ‘3d‘ ‘删除第 3 行‘
...省略内容
[root@localhost ~]# nl test.txt | sed ‘3,5d‘ ‘删除第 3~5 行‘
...省略内容
[root@localhost ~]# nl test.txt | sed ‘/cross/d‘ ‘删除包含cross 的行‘
...省略内容
[root@localhost ~]# nl test.txt | sed ‘/cross/! d‘ ‘删除不包含cross 的行‘
...省略内容
[root@localhost ~]# sed ‘/^[a-z]/d‘ test.txt ‘删除以小写字母开头的行‘
...省略内容
[root@localhost ~]# sed ‘/\.$/d‘ test.txt ‘删除以"."结尾的行‘
...省略内容
[root@localhost ~]# sed ‘/^$/d‘ test.txt ‘删除所有空行‘
...省略内容
[root@localhost ~]# sed -e ‘/^$/{n;/^$/d}‘ test.txt ‘删除重复的空行,即连续的空行只保留一个,效果与“cat -s test.txt”相同,n 表示读下一行数据‘
...省略内容
3.3.3、s - 替换符合条件的文本
[root@localhost ~]# sed ‘s/the/THE/‘ test.txt ‘将每行中的第一个the 替换为 THE ‘
[root@localhost ~]# sed ‘s/l/L/2‘ test.txt ‘将每行中的第 2 个l 替换为L ‘
[root@localhost ~]# sed ‘s/the/THE/g‘ test.txt ‘将文件中的所有the 替换为THE‘
[root@localhost ~]# sed ‘s/o//g‘ test.txt ‘将文件中的所有o 删除(替换为空串)‘
[root@localhost ~]# sed ‘s/^/#/‘ test.txt ‘在每行行首插入#号‘
[root@localhost ~]# sed ‘/the/s/^/#/‘ test.txt ‘在包含the 的每行行首插入#号‘
[root@localhost ~]# sed ‘s/$/EOF/‘ test.txt ‘在每行行尾插入字符串EOF‘
[root@localhost ~]# sed ‘3,5s/the/THE/g‘ test.txt ‘将第 3~5 行中的所有the 替换为 THE‘
[root@localhost ~]# sed ‘/the/s/o/O/g‘ test.txt ‘将包含the 的所有行中的o 都替换为 O‘
[root@localhost ~]# sed ‘/the/{H;d};$G‘ test.txt ‘将包含the 的行迁移至文件末尾,{;}用于多个操作‘
[root@localhost ~]# sed ‘1,5{H;d};17G‘ test.txt ‘将第 1~5 行内容转移至第 17 行后‘
[root@localhost ~]# sed ‘/the/w out.file‘ test.txt ‘将包含the 的行另存为文件out.file ‘
[root@localhost ~]# sed ‘/the/r /etc/hostname‘ test.txt ‘将文件/etc/hostname 的内容添加到包含the 的每行以后‘
[root@localhost ~]# sed ‘3aNew‘ test.txt ‘在第 3 行后插入一个新行,内容为 New ‘
[root@localhost ~]# sed ‘/the/aNew‘ test.txt ‘在包含the 的每行后插入一个新行,内容为 New‘
[root@localhost ~]# sed ‘3aNew1\nNew2‘ test.txt ‘在第 3 行后插入多行内容,中间的\n 表示换行‘
3.3.5、f - 使用脚本编辑文件
[root@localhost ~]# sed ‘1,5{H;d};17G‘ test.txt ‘将第 1~5 行内容转移至第 17 行后‘
‘以上操作可以改用脚本文件方式:‘
[root@localhost ~]# vim abc.list ‘编辑指令放到/abc.list中‘
1,5H
1,5d
17G
[root@localhost ~]# sed -f abc.list test.txt ‘使用abc.list文件指令编辑test.txt文件‘
原文:https://www.cnblogs.com/liuwei186/p/13915544.html