Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bash脚本:全部更改&;至&;amp;在所有文件中_Bash - Fatal编程技术网

bash脚本:全部更改&;至&;amp;在所有文件中

bash脚本:全部更改&;至&;amp;在所有文件中,bash,Bash,我有一个包含很多XML文件的文件夹。 其中一组中有&,应将其转换为& 我不是bash大师,但我能用bash脚本以某种方式更改所有文件中的所有字符吗?您只需将文件通过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 lin

我有一个包含很多XML文件的文件夹。 其中一组中有
&
,应将其转换为
&


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

您只需将文件通过
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可以为您在当前工作目录中的所有文件上进行就地替换

sed -i 's/&/&/g' *
如果你想让它变成多层次的,比如

for file in `find`; do sed -i 's/&/&/g' $file; done
如果您只想替换扩展名为.xml的文件(这可能很有用),请执行以下操作

for file in `find -iname '*.xml'`; do sed -i 's/&/&/g' $file; done

不过,这将使所有其他命名实体都出错。例如,将成为&;lt@金姆,会的。可能最快的修复方法是通过一系列反转操作传递结果文件,如:
s/&;lt//g
for file in `find -iname '*.xml'`; do sed -i 's/&/&/g' $file; done
!/bin/bash
startdirectory="/home/jack/tmp/tmp2"
searchterm="&"
replaceterm="&"
        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!"