sed简记
更新日期:
文章目錄
sed是linux 下的一个文本编辑命令,它功能强大,经常被用来编辑或分析处理一个或多个文件,简化对文件的处理。它和awk的处理方式差不多,都是对输入一行一行的处理,然后输出。
-n 只显示被处理的行,其他行不显示,常用
-e 用来执行多个指令,如 nl /etc/passwd | sed -e ‘2d’ -e ‘/root/{s/bash/python/}’
-f 执行脚本命令,可以把复杂指令写到文件里,通过sed -f name执行
-r 执行扩展正则表达式
-i 直接修改文件,而不是输出到屏幕,慎用
action’
a 新增 sed ‘1a hello world’ test.txt 在第一行之后新加一行
c 取代 sed ‘2,5c hello world’ test.txt 把2到5行内容替换为hello world
d 删除 sed ‘1d’ test.txt 删除第一行
sed ‘/root/d’ test.txt 删除匹配root的行
i 新增 sed ‘1a hello world’ test.txt 在第一行之前新加一行
p 打印 sed -n ‘1,10p’ test.txt 打印1到10行
sed -n ‘1,10!p’ test.txt 打印1到10行之外的所有行
s 替换 sed ‘s/hello/world/g’ test.txt 把文件中的hello全部替换为world
q 退出 sed -n ‘10p’ test.txt 打印10行退出