sed是一种流编辑器,它是文本处理中非常有用的工具
能够完美的配合正则表达式使用,处理时,把当前处理的行存储在临时缓冲区中,称为模式空间,接着用sed命令处理缓冲区中的内容
处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变
sed的特点:
sed命令是将一系列的编辑命令应用于一批文本的理想工具
sed命令是一个非交互式的文本编辑器,它可以对来自文本文件以及标准输入的文本进行编辑。其中,标准输入可以是来自键盘、文件重定向、字符串、变量或者是管道的文本
sed命令会从文件或者标准输入中一次读取一行数据,将其复制到缓冲区(最多8192字节),然后读取命令行或者脚本的编辑子命令,对缓冲区中的文本行进行编辑。重复此过程,一直到所有的文本行都处理完毕
语法:
sed OPTIONS… [SCRIPT] [INPUTFILE…] 参数:
| 常用的选项 | 说明 |
|---|---|
| -n,--quiet,--silent | 只打印匹配的行 |
| -i | 直接编辑原文件,而不是由屏幕输出,默认不对原文件进行操作; |
| -e | 直接在命令行模式上进行sed的动作编辑,不会对原文件修改 |
| -r | 使用扩展正则表达式 |
| -f | 直接将sed的动作写在一个文件内,-f filename则可以执行filename内的sed动作 |
[root@quruixiang ~]# sed -n '3p' /etc/passwd 地址定界:(查询)
- # 1)#:#为数字,指定要进行处理操作的行;1,表示第一行;
- [root@quruixiang ~]# sed -n '3,4p' /etc/passwd
-
- # 2)$:表示最后一行,多个文件进行操作的时候,为最后一个文件的最后一行;
- [root@quruixiang shellstudy]# sed -n '$p' test8.sh
-
- # 3)/regexp/:表示能够被regexp匹配到的行;
- [root@quruixiang ~]# sed -n "/test/p" /etc/passwd
-
- ## regexp即基于正则表达式的匹配;
- # 4)/regexp/I:匹配时忽略大小写;
- [root@quruixiang shellstudy]# sed -n "/test/Ip" /etc/passwd
-
- # 5)\%regexp%: 任何能够被regexp匹配到的行,换用%(用其他字符也可以,如:#)为边界符号;
- [root@quruixiang shellstudy]# sed -n "\%echo%p" test8.sh
-
- # 6)addr1,addr2:指定范围内的所有的行(范围选定);
- [root@quruixiang ~]# sed -n "3,6p" /etc/passwd
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
- adm:x:3:4:adm:/var/adm:/sbin/nologin
- lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- sync:x:5:0:sync:/sbin:/bin/sync
-
- # 常用地址定界表示方式:
- ## a)0,/regexp/:从起始行开始到第一次能够被regexp匹配到的行。
- [root@quruixiang shellstudy]# sed -n "0,/bin/p" test8.sh
-
- ## b)/regexp/,/regexp/:被模式匹配到的行内的所有的行。
- ## 7)first~step:指定起始的位置及步长,例如:1~2表示1,3,5…
- [root@quruixiang shellstudy]# sed -n "1~3p" test8.sh
- #!/bin/bash
- #Version:v1.0
- #Description:
- func()
- echo 1
- fi
- echo $result
- ## 8)addr1,+N:指定行以及以后的N行;
- [root@quruixiang ~]# sed -n "3,+4p" /etc/passwd
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
- adm:x:3:4:adm:/var/adm:/sbin/nologin
- lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- sync:x:5:0:sync:/sbin:/bin/sync
- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
-
- ### addr1,~N:addr1开始的行,N结束的行
- [root@quruixiang ~]# sed -n "3,~4p" /etc/passwd
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
- adm:x:3:4:adm:/var/adm:/sbin/nologin
注意事项:
如果没有指定地址,表示命令将应用于每一行
如果只有一个地址,表示命令将应用于这个地址匹配的所有行
如果指定了由逗号分隔的两个地址,表示命令应用于匹配第一个地址和第二地址之间的行(包括这两行)
如果地址后面跟有感叹号,表示命令将应用于不匹配该地址的所有行
常用编辑命令:(编辑)
- # 1)d:删除匹配到的行
- [root@quruixiang shellstudy]# sed '3d' test8.sh
- [root@quruixiang shellstudy]# sed '/echo/d' test8.sh
- [root@quruixiang shellstudy]# sed '/^$/d' test8.sh # 删除空行
-
- # 2)p:打印当前模式空间内容
- [root@quruixiang shellstudy]# sed '/echo/p' test8.sh
-
- # 3)a \text:append,表示在匹配到的行之后追加内容
- [root@quruixiang shellstudy]# sed '$a\system' test8.sh
- [root@quruixiang shellstudy]# sed '3a\systemctl' test8.sh
- [root@quruixiang shellstudy]# sed '/echo/a\systemctl' test8.sh
-
- # 4)i \text:insert,表示在匹配到的行之前追加内容
- [root@quruixiang shellstudy]# sed '3i\sys' test8.sh
- [root@quruixiang shellstudy]# sed '/echo/i\sys' test8.sh
-
- # 5)c \text:change,表示把匹配到的行和给定的文本进行交换
- [root@quruixiang shellstudy]# sed '/echo/c\system' test8.sh
-
- # 6)s/regexp/replacement/flages:查找替换,替换regexp匹配到的内容(其中/可以用其他字符代替,
- sed -i '/^SELINUX=/ s/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
- [root@quruixiang shellstudy]# sed 's/fun/##/1' test8.sh # 替换每行中第一个匹配的fun替换为##
- ## 例如@)
- ## 其他编辑命令:
- ## 常用的flages:
- ## g:全局替换,默认只替换第一
- [root@quruixiang shellstudy]# sed 's/echo/system/g' test8.sh
- [root@quruixiang shellstudy]# sed 's/^/##/g' test8.sh # 文件中的每一行开头加一个##
- [root@quruixiang shell]# sed -i 's/^#[[:space:]]*//g' test.txt # 删除开头#和至少一个空字符的行
- ## i: 不区分大小写
- ## p:如果成功替换则打印
- [root@quruixiang shellstudy]# sed 's/echo/system/p' test8.sh
-
- # 7)r 读入文件内容追加到匹配行后面
- [root@quruixiang shellstudy]# sed 'r 2.txt' 1.txt # 在1.txt中的每一行后都写入2.txt的内容
- [root@quruixiang shellstudy]# sed '3r 2.txt' 1.txt # 在1.txt中的第3行后写入2.txt的内容
- [root@quruixiang shellstudy]# sed '/245/r 2.txt' 1.txt # 在1.txt中的匹配行后写入2.txt的内容
-
- # 8)R 读入文件一行内容追加到匹配行后面
- # 9)y :y/source/dest/ 固定长度替换,要求替换的字符串长度相等
- # 10)w /path/to/somefile:将匹配到的文件内容追加到指定的文件末尾
- [root@quruixiang shell]# sed -n 'w test.txt' passwd # 在test.txt的末尾0后写入passwd的内容
| 特殊符号 | 说明 |
|---|---|
| ! | 对指定行以外的所有行应用命令 |
| = | 打印当前行行号 |
| ~ | “first~step”表示从first行开始,以步长step递增 |
| & | 代表被替换的内容 |
| ; | 实现一行命令语句可以执行多条sed命令 |
| {} | 对单个地址或地址范围执行批量操作 |
| + | 地址范围中用到的符号,做加法运算 |
- # 打印匹配到echo的行号
- [root@quruixiang shellstudy]# sed -n '/echo/=' test8.sh
- # 打印最后一行的行号
- [root@quruixiang shellstudy]# sed -n '$=' test8.sh
- # 删除3之外的所有行
- [root@quruixiang shellstudy]# sed '3!d' test8.sh
- # 删除1~3之外的所有行
- [root@quruixiang shellstudy]# sed '1,3!d' test8.sh
- # 删除匹配到fi到最后一行之外的所有行
- [root@quruixiang shellstudy]# sed '/fi/,$!d' test8.sh
- # 删除从匹配fi的行到最后一行
- [root@quruixiang shellstudy]# sed '/fi/,+1d' test8.sh
- # 删除匹配echo或fi的行
- [root@quruixiang shellstudy]# sed '/echo\|fi/d' test8.sh
- # 删除1~3行中,匹配内容bin的行
- [root@quruixiang shellstudy]# sed '1,3{/bin/d}' test8.sh
- # 打印匹配echo的行的行号和内容
- [root@quruixiang shellstudy]# sed -n '/echo/{=;p}' test8.sh
- # 打印3行到10行的内容
- [root@quruixiang shell]# sed -n '3,10{=;p}' test.txt