有关sed命令进行文本替换的一些例子,sed替换给定文本中的字符串,sed替换文本中所有内容,sed命令参数选项用法。
1、sed替换给定文本中的字符串。
sed 's/pattern/replace_string/' file
2、将sed替换结果应用于原文件。
sed -i 's/text/replace/' file
3、sed替换掉所有内容,加上参数g.
sed 's/pattern/replace_string/g' file
sed 's/pattern/replace_string/3g' file
4、sed移除空白行
sed '/^$/d' file
5、sed使用指定数字替换文件中所有3位数的数字:
sed -i 's/\b[0-9]\{3\}\b/NUMBER/g'
6、已匹配字符串标记(&)
echo this is an example |sed 's/\w\+/[&]/g'
7、字串匹配标记(\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/" (责任编辑:IT) |