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

发布于 2024-08-04 13:01:33 字数 364 浏览 6 评论 0原文

我有大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半岛未凉 2024-08-11 13:01:33

您必须担心要替换的文本中的斜杠。

  • 或者:对每个斜杠使用“\/”,
  • 或者:作弊并使用“.”来匹配斜杠应出现的位置处的任何字符。

另一种方法利用了包含 HTML 的文件的可能性。理论上,如果您不喜欢第二种选择,则还应该在您正在查看的字符串中出现“.”的每个点处使用“\.” 。

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

这是从您的示例中复制的,斜杠被点替换。但是,像这样在命令行上提供所有文件名只会将输出作为所有已编辑文件的串联写入到标准输出。

传统上,要或多或少地在原位编辑文件,您可以这样写:

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

这包括对临时文件进行合理的防弹清理 - 它并不完美。此变体会先备份原始版本,然后再将其替换为编辑后的版本:

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

使用 GNU sed,您可以使用“-i”或“--in-place”' 覆盖文件的选项;您可以使用“--in-place=.bak”为 file.bak 中的每个文件创建备份副本。

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. 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; you can use '--in-place=.bak' to create backup copies of each file in file.bak.

執念 2024-08-11 13:01:33

您需要使用额外的反斜杠转义特殊字符。

请注意,输出也将全部发送到控制台。如果您想要 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!

无人接听 2024-08-11 13:01:33

你应该通过管道输出
例如

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

You should pipe the output
e.g.

sed -e 's/<.*SRC="\/height.js".*>//g' < foo.html > foo1.html
幽梦紫曦~ 2024-08-11 13:01:33

您不必将 / 与 s 命令一起使用:

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

如果您想进行内联替换,那么第一步是备份所有文件,然后发出此命令,替换 'old ' 和 'new' 以及您选择的字符串:

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

因为内联替换非常危险,所以我必须强调您必须在运行命令之前备份所有文件

我是否提到过您应该备份所有文件

You don't have to use the / with the s command:

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:

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文