在 shell 中使用 grep 和 sed 替换单词
我有大约 150 个文件,我想在其中删除以下代码:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="/height.js"></SCRIPT>
我正在做的是:
sed -e 's/<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="/height.js"></SCRIPT>/ /' file_names
这似乎不起作用。
我想一次性从所有文件中删除这个脚本。我怎样才能做到这一点?
I have some 150 files and in them I want to remove this following code:
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须担心要替换的文本中的斜杠。
\/
”,.
”来匹配斜杠应出现的位置处的任何字符。另一种方法利用了包含 HTML 的文件的可能性。理论上,如果您不喜欢第二种选择,则还应该在您正在查看的字符串中出现“
.
”的每个点处使用“\.
” 。这是从您的示例中复制的,斜杠被点替换。但是,像这样在命令行上提供所有文件名只会将输出作为所有已编辑文件的串联写入到标准输出。
传统上,要或多或少地在原位编辑文件,您可以这样写:
这包括对临时文件进行合理的防弹清理 - 它并不完美。此变体会先备份原始版本,然后再将其替换为编辑后的版本:
使用 GNU
sed
,您可以使用“-i
”或“--in-place”
' 覆盖文件的选项;您可以使用“--in-place=.bak
”为file.bak
中的每个文件
创建备份副本。You have to worry about the slashes in the text you are replacing.
\/
' for each slash,.
' to match any character at the point where the slash should appear.The alternative exploits the improbability of a file containing the 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.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:
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:
With GNU
sed
, you can use the '-i
' or '--in-place
' option to overwrite the files; you can use '--in-place=.bak
' to create backup copies of eachfile
infile.bak
.您需要使用额外的反斜杠转义特殊字符。
请注意,输出也将全部发送到控制台。如果您想要 150 个单独的输出文件,您可能需要查看 xargs 命令,例如:
ls -1 | xargs -t -i 'sed -e -i "replace comment" {}'
请注意,sed '-i' 选项将就地编辑文件,因此请先正确替换并备份文件!
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:
ls -1 | xargs -t -i 'sed -e -i "replace comment" {}'
Be aware that the sed '-i' option will edit the files in place so get your replacement right first and back the files up!
你应该通过管道输出
例如
You should pipe the output
e.g.
您不必将 / 与 s 命令一起使用:
如果您想进行内联替换,那么第一步是备份所有文件,然后发出此命令,替换 'old ' 和 'new' 以及您选择的字符串:
因为内联替换非常危险,所以我必须强调您必须在运行命令之前备份所有文件。
我是否提到过您应该备份所有文件?
You don't have to use the / with the s command:
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:
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?