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/"
转载于:https://blog.51cto.com/masters/1436245