Stream editor
用来操作纯SCLL码的文本,处理时,把当前的行储存在临时缓存区中,称为“模式空间(pattern space)”可以指定仅仅处理哪些行,sed符合模式条件的处理,不符合条件的不处理,处理完成后把缓存区的内容送往屏幕,接着处理下一行,这样不断重复,直到文件末尾。
2.Sed 对字符的处理:
p | 显示模式 |
---|---|
d | 删除模式 |
a | 添加模式 |
c | 替换模式 |
w | 写入模式 |
i | 插入模式 |
2.1 、P(显示)模式
sed -n ‘/^#/p’ fstab //显示以“#”开头的行
sed -n ‘/UUID$/P’ fstab //显示以UUID结尾的行sed -n ‘2,6P’ fstab //显示2到6行
sed -n ‘2,6!P’ fstab //不显示2到6行
sed -n ‘2p;6p’ fstab //显示第2行和第6行
2.2 、D(删除)模式
sed -n ‘/^#/d’ fstab //删除以“#”开头的行
sed -n ‘/UUID$/d’ fstab //删除以UUID结尾的行
sed -n ‘2,6d’ fstab //显示2到6行
sed -n ‘2,6!d’ fstab //删除2到6行
sed -n ‘2p;6d’ fstab //删除第2行和第6行
2.3 、a(添加)模式
sed -e ’/^UUID/ahello’ fstab //在以UUID开头的行后面体添加一行hello
sed -e ’/^UUID/ahello\nword’ fstab //在以UUID开头的行后面体添加一行hello再换行添加world。
2.4 、c(替换)模式
sed -e ‘/^UUID/chello’ fstab v//将以UUID开头的行替换为hello
2.5 、w(写入)模式
sed -e ‘/^UUID/wfile.txt’ fstab //将fstab文件中以UUID开头的行写入file.txt文件中
sed -n ‘/^UUID/w /mnt/test’ fstab //同上,但-n没有输出,只显示处理结果sed ‘/^UUID/=’ fstab //将文件fstab以UUID开头的行号输出,=表示加行号
sed ‘1r /mnt/hello’ fstab //将/mnt/hello文件写入fstab的第一行
sed ‘$r /mnt/hello’ fstab //将/mnt/hello文件写入fstab的最后一行
2.6 、i(插入)模式
sed -e '/westos/iworld' westos3.其他参数
sed -n -f prctise fstab // 对文件执行prctise的策略
/^UUID/p //文件内容
/^UUID/= //文件内容
sed -n -e ‘/^UUID/p’ -ne ‘/^UUID/=’ fstab //同上
sed -n -e ‘/^UUID/p;/^UUID/=’ fstab
sed ‘s/w/W/g’ fstab 将全文w换成W,与sed ‘s@w@W@g’ fstab // 等同
sed ‘1,3s/w/W/g ’ fstab 将前3行的w换成W
sed ‘/adm/,/sync/s/nologin/westos/g’ //passwd 替换adm到sync中间的nologin为westos
sed ‘G’ fstab //G加空行,在每一行后面加空行
sed ‘$!G’ //fstab 在最后一行不加空行
sed ‘!G’ fstab //不加空行
sed ‘=’ fstab 显示行号