sed命令地址后面跟有感叹号(!),那么命令将应用于不匹配该地址的所有行
/^\.TS/,/^\.TE/!d
sed 分组命令 {}
右大括号本身单独一行,且无空格
/^\.TE/,/^\.TF/{ /^$/d s/china/China/ }
sed使用字符类
cat test 1 a 2 b d 3 g h
sed 's/[[:digit:]]/shell/' test shell a shell b d shell g h
替换命令s
[address]s/pattern/replacement/flags
flags有如下可以使用
n 对匹配的第几次进行替换 g 全局更改 p 打印模式空间 w file 写到文件
在replacement部分,以下字符有特殊含义
& 用前面正则匹配的内容替换 \n 匹配第n个子串 \ 转义特殊字符
删除命令 d : 会导致读取新的输入行。
追加 a\ 插入 i\ 更改 c\
追加和插入命令只用于单行地址
$是行寻址符号,用户匹配文件的最后一行
读和写
读命令r,不能对一个范围行进行操作
cat example 1 2 3 aa 3 4 aa
sed ' /[[:alpha:]]/{ r test /[[:alpha:]]/d} ' example 1 2 3 apple ipad2 iphone 3 4 apple ipad2 iphone
退出 q
只适用于单行地址
#打印前100行 sed '100q' example
高级sed命令
处理多行模式空间 N D P 保持空间 H h G g x 条件和分支 : b t