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 '/^$/d' file

  5. 使用指定的数字替换文件中所有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/'

  8. 引用

    text=hello

    echo "hello world." |sed "s/$text/HELLO/"