2011-05-25 31 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!"