2012-11-08 100 views

回答

0

用sed:

SED的/源/目的/'file_with_sentence.txt

  • 来源是搜索词
  • destiantion是更换
  • file_with_sentence ...是海基会解释

你可以将它包装到一些unix shell脚本中(你没有告诉哪个shel l为你使用)

甚至更​​好:

回声 “这是我的一句” | SED的/源/目的/'

1

如果你需要一个命令来做到这一点,尝试SED。

例如,要在句子hello worldgoodbye替换hello

$ sed 's/hello/goodbye/g' <<< "hello world" 
goodbye world 

如果你想在这个bash脚本:

#!/bin/bash 

if [[ $# -lt 3 ]] 
then 
    echo "Usage: $(basename $0) word replacement sentence" >&2 
    exit 1 
fi 

word="$1" 
replacement="$2" 
sentence="$3" 

echo "${3//$1/$2}" 

例子:

$ replace.sh hello goodbye "hello world" 
goodbye world