2011-05-25 63 views
2

我有一个包含大量XML文件的文件夹。 其中有一堆有&,应该转换为&bash脚本:全部更改为&

我不是bash大师,但我可以用bash脚本以某种方式更改所有文件中的所有文件?

回答

3

您只需通过sed过滤器来传递文件,如下面的成绩单:

$ echo ' 
this is line 1 
this is line 2 with a & character. 
and this is line 3 with & and & on it' | sed 's/&/&/g' 

this is line 1 
this is line 2 with a & character. 
and this is line 3 with & and & on it 

要使用一组文件做到这一点,你可以(当然有备份)使用就地变种:

sed -i.bak 's/&/&/g' *.xml 
+0

'sed的-i'在原地进行编辑。 – 2011-05-25 08:11:58

+0

真棒谢谢:D – superbly 2011-05-25 08:24:19

+1

这会搞砸所有其他命名实体。例如,<将变为& lt; – 2011-05-25 09:08:52

3

的sed可以在当前工作目录中的所有文件为你做的就地更换,

sed -i 's/&/&amp;/g' * 

如果你希望它去的多级系统,像

for file in `find`; do sed -i 's/&/&amp;/g' $file; done 

如果你只希望做与.xml扩展,这可能是有用的文件替换,做

for file in `find -iname '*.xml'`; do sed -i 's/&/&amp;/g' $file; done 
2
!/bin/bash 
startdirectory="/home/jack/tmp/tmp2" 
searchterm="&" 
replaceterm="&amp;" 
     for file in $(grep -l -R $searchterm $startdirectory) 
      do 
      sed -e "s/$searchterm/$replaceterm/ig" $file > /tmp/tempfile.tmp 
      mv /tmp/tempfile.tmp $file 
      echo "Modified: " $file 
     done 
echo "Done!"