-n 只显示匹配的行bash
-r 可不脱意.net
打印字符串
sed -n '5'p test.txt 打印第5行get
sed -n '1,5'p test.txt 打印1-5行test
sed -n '1,$'p test.txt 打印1-最后行sed
sed -n '/root/'p test.txt 打印含有root的行file
sed -n '/^1/'p test.txt 打印1开头的行文件
sed -n 'in$'p test.txt 打印in结尾的行字符
sed -n '/r..o/'p test.txt 打印r..o 中间任意2个字符的行数字
sed -n 'oo*'p test.txt 打印oo匹配0次或屡次,后面跟p的行
sed -e '1'p -e '/111/'p -n test.txt 多条件匹配打印(或者关系, 或重合屡次打印,符合条件1打印1次,符合条件2又打印1次)
sed -nr ‘/o{2}/’p filename 打印符合 oo出现2次的内容 r 不脱意
sed -n ‘/bus/’Ip test I不区分大小写,大写的I
删除
-i 真正更改文件的内容
sed '1'd test.txt 删除第一行
sed '1,3'd test.txt 删除1-3行
sed '/oot/'d test.txt 删除含有oot的行
sed ‘/user2/’d test.txt 删除 user2 相关的行
sed -i ‘1,10’d test.txt 删除1-10行(i直接删)
替换
sed '1,2s/ot/to/g' test.txt 把1-2行 ot替换成to g表明全局替换
sed '1,10s/root/toor/g’ test.text 替换1-10行符合root, 替换成toor
sed -r '1,10s/ro+'/r/g 把ro+替换成r , 用+需用-r
sed 's#ot#to#g' test.txt 用#代替/
sed 's/[0-9]//g' test.txt 把数字删除(替换成空)
sed 's/[a-zA-Z]//g' test.txt 把字母删除(替换成空)
sed -r 's/(rot)(.*)(bash)/\3\2\1/' test.txt
sed 's/^.*$/123&/' test.txt 把每行前面加123, &表示前面匹配到的内容
sed -i 's/ot/to/g' test.txt
head passwd | sed -r ‘s/([^:]+):(.*):([^:]+)/\3:\2:\1/’ 把第1行和最后1行互换
sed ‘s//root/123/g’ passwd | head 替换内容中含有/符号的替换, 需用\脱意
sed ‘s//sbin/nologin/123/g’ passwd | head 替换/sbin/nologin为123(替换内容中含有/符号的替换)
sed ‘s@/sbin/nologin@123 @g’ passwd | head 替换/sbin/nologin为123(替换内容中含有/符号的替换的另一种写法, 换个分隔符)
head passwd | sed -r ‘s/(.)/aaa:&/’ 在全部行前面加一个固定字符串, &等于\1, (.)表示一整行