2012-06-01 286 views

回答

20

当然,你可以使用sed或awk来做到这一点。 sed示例:

sed -i 's/Andrew/James/g' /home/oleksandr/names.txt 
0

通常使用awk或sed这样的工具。

$ sed -i 's/ugly/beautiful/g' /home/bruno/old-friends/sue.txt 
1

您可以用sed来做到这一点:

sed -i 's/toreplace/yoursetting/' configfile 

sed的可能是目前在每一个类Unix系统在那里。如果您要更换多个occurence可以在G添加到S-命令:

sed -i 's/toreplace/yoursetting/g' configfile 

要小心,因为这会彻底摧毁你的CONFIGFILE如果不正确地指定toreplace价值。 sed还支持搜索和替换中的正则表达式。

3
sed -i 's/variable/replacement/g' *.conf 
6

您可以使用sed执行搜索/替换。我通常是从一个bash shell脚本做到这一点,并移动原始文件包含的值被取代为新的名称,然后运行的sed写输出到我原来的文件名是这样的:

#!/bin/bash 
mv myfile.txt myfile.txt.in 

sed -e 's/PatternToBeReplaced/Replacement/g' myfile.txt.in > myfile.txt. 

如果你不” t指定一个输出,替换将转到stdout。

1
filepath="/var/start/system/dir1" 
searchstring="test" 
replacestring="test01" 

i=0; 

for file in $(grep -l -R $searchstring $filepath) 
do 
    cp $file $file.bak 
    sed -e "s/$searchstring/$replacestring/ig" $file > tempfile.tmp 
    mv tempfile.tmp $file 

    let i++; 

    echo "Modified: " $file 
done 
+0

你可以在你的代码中检查'输入代码在这里吗?它可能不应该在那里。顺便说一下:在大多数情况下,在解决方案中提供注释是一个好主意,或者至少总结您选择解决问题的方法。 –