Linux命令之——替换文件里的字符串


Linux命令之——替换文件里的字符串
 
在Linux中,如果需要替换一个文本文件里的某个字符串,需要用到sed这个命令。下面给出例子:
 
 要把 test.txt文件里的所有 desc 字符串替换为 helloworld,可用这样的命令
 
 sed "s#desc#helloworld#g" test.txt >test.txt.temp
 
 mv test.txt.temp test.txt
 
于是可以做出一个替换字符串的函数。
 
[plain] 
function replacePara()  
  
{  
  
      old=$1  
  
      new=$2  
  
       file=$3  
  
       sed "s#$old#$new#g" $file>$file.temp  
  
       mv $file.temp $file  
  
}  
 
 
然后调用方法
 
replacePara desc helloworld test.txt
 
即可将test.txt里的desc全部替换为helloworld

相关内容

    暂无相关文章