sed可以替换给定文本中的字符串。
sed 's/pattern/replace_string/' file
将sed替换结果应用于原文件。
sed -i 's/text/replace/' file
使用sed需要替换掉所有内容,需要在尾部加上参数g.
sed 's/pattern/replace_string/g' filesed 's/pattern/replace_string/3g' file
移除空白行
sed '/^$/d' file
使用指定的数字替换文件中所有3位数的数字:
sed -i 's/\b[0-9]\{3\}\b/NUMBER/g'
已匹配字符串标记(&)
echo this is an example |sed 's/\w\+/[&]/g'
字串匹配标记(\1)
echo this is digit 7 in a number |sed 's/digit \([0-9]\)/\1/'
echo sever EIGHT |sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
引用
text=hello
echo "hello world." |sed "s/$text/HELLO/"
使用sed进行文本替换
原创
©著作权归作者所有:来自51CTO博客作者mastters的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:用grep在文件中搜索文本
下一篇:使用awk进行高级文本处理
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
MySQL sed mysql sed -i
shell 文件内容替换
sed bc ruby