定义:Sed 是针对数据流的非交谈式编辑器,它在命令行下输入编辑命令并指定文件,然后可以在屏幕上看到编辑命令的输出结果。
好处:Sed 在缓冲区内默认逐行处理数据,所以源文件不会被更改和破坏。
格式:Sed ‘command' filename
原理:Sed 用数字或规则表达式在定位编辑的地址,然后Command告诉Sed如何处理该行。可以打印,删除,或者更改。
#/bin/bash echo 'here is a sed script' echo '======= print the content of employees' cat employees echo '======= delete line1 to 3 and print' sed '1,3d' employees #delete line include frank sed '/frank/d' employees #delete line unclude frank sed '/frank/!d' employees #print frank line and all line sed '/frank/p' employees #print frank line only sed -n '/frank/p' employees #delete line4 to end and print sed '4,$d' employees #delete last ine and print sed '$d' employees #delete line including john and print sed '/john/d' employees #replace all john to hello sed 's/john/hello/g' employees #replace all john to hello and print replaced only sed -n 's/john/hello/gp' employees #print replaced line only sed -n 's/^john/hello/p' employees #replace nn to nn.5 and print sed 's/[0-9][0-9]$/&.5/' employees #print replaced line only sed -n 's/[0-9][0-9]$/&.5/p' employees #print from eric to mark sed -n '/eric/,/mark/p' employees #print line1 to mark sed -n '1,/^mark/p' employees #delete line and replace sed -e '1,3d' -e 's/frank/huma/' employees