简体   繁体   English

使用grep替换单词并在shell中使用sed

[英]replace words using grep and sed in shell

I have some 150 files and in them I want to remove this following code: 我有150个文件,在其中我要删除以下代码:

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="/height.js"></SCRIPT>

What I'm doing is: 我正在做的是:

sed -e 's/<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="/height.js"></SCRIPT>/ /' file_names

This doesn't seem to work. 这似乎不起作用。

I want to remove this script from all the files in one go. 我想一次性从所有文件中删除此脚本。 How can I do that? 我怎样才能做到这一点?

You have to worry about the slashes in the text you are replacing. 您必须担心要替换的文本中的斜杠。

  • Either: use ' \\/ ' for each slash, 要么:对每个斜杠使用“ \\/ ”,
  • Or: cheat and use ' . 或者:作弊并使用' . ' to match any character at the point where the slash should appear. '以匹配应出现斜线的任何字符。

The alternative exploits the improbability of a file containing the HTML. 替代方法利用了包含HTML的文件的可能性。 Theoretically, if you don't like the second alternative, you should also use ' \\. 从理论上讲,如果您不喜欢第二种选择,则还应使用' \\. ' at each point where ' . 在每个地方. ' appears in the string you're looking at. '出现在您要查看的字符串中。

sed -e 's/<SCRIPT LANGUAGE="JavaScript" TYPE="text.javascript" SRC=".height.js"><.SCRIPT>/ /' file_names

This is copied from your example and slashes are replaced by dots. 这是从您的示例复制而来的,斜杠由点代替。 However, supplying all the file names on the command line like that will simply write the output as the concatenation of all the edited files to standard output. 但是,像这样在命令行上提供所有文件名,只会将输出作为所有已编辑文件的串联写入标准输出。

Classically, to edit files more or less in situ, you'd write: 传统上,要或多或少就地编辑文件,可以这样写:

tmp=${TMPDIR:-/tmp}/xxx.$$
trap 'rm -f $tmp; exit 1' 0 1 2 3 13 15
for file in ...list...
do
    sed -e '...' $file > $tmp
    mv $tmp $file
done
rm -f $tmp
trap 0

This includes reasonably bullet-proof clean-up of the temporary - it is not perfect. 这包括对临时工进行合理的防弹清理-这并不完美。 This variant backs up the original before replacing it with the edited version: 此变体将备份原始版本,然后将其替换为编辑版本:

tmp=${TMPDIR:-/tmp}/xxx.$$
trap 'rm -f $tmp; exit 1' 0 1 2 3 13 15
for file in ...list...
do
    sed -e '...' $file > $tmp
    mv $file $file.bak
    mv $tmp $file
done
rm -f $tmp
trap 0

With GNU sed , you can use the ' -i ' or ' --in-place ' option to overwrite the files; 使用GNU sed ,您可以使用-i--in-place选项覆盖文件。 you can use ' --in-place=.bak ' to create backup copies of each file in file.bak . 您可以使用' --in-place=.bak '在file.bak创建每个file备份副本。

You need to escape the special characters with an extra backslash. 您需要使用额外的反斜杠来转义特殊字符。

Note that the output will also all go to the console. 请注意,输出还将全部发送到控制台。 If you want 150 separate output files, you might want to look at the xargs command, something like: 如果需要150个单独的输出文件,则可能需要查看xargs命令,如下所示:

ls -1 | ls -1 | xargs -t -i 'sed -e -i "replace comment" {}' xargs -t -i'sed -e -i“替换注释” {}'

Be aware that the sed '-i' option will edit the files in place so get your replacement right first and back the files up! 请注意,sed'-i'选项将在适当位置编辑文件,因此请先进行替换,然后再备份文件!

您应该通过管道输出,例如

sed -e 's/<.*SRC="\/height.js".*>//g' < foo.html > foo1.html

You don't have to use the / with the s command: 您不必在s命令中使用/:

sed 's|old|new|' files...

If you want to do inline-replace, then the first step is to back up all of your files then issue this command, substitute 'old' and 'new' with strings of your choice: 如果要进行内联替换,则第一步是备份所有文件,然后发出以下命令,将“ old”和“ new”替换为您选择的字符串:

sed -i .bak 's|old|new|' files...

Because inline replacement is very dangerous, I can't emphasize enough that you must back up all of your files before running the command. 因为内联替换非常危险,所以我不能强调必须在运行命令之前备份所有文件

Did I mention that you should back up all of your files ? 我是否提到过应该备份所有文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM