组合两个 sed 命令

发布于 2024-12-08 09:32:44 字数 614 浏览 0 评论 0原文

我有一个文件 r.我想将其中的单词 FileMINvac.pdb 替换为空。我使用的命令是

sed -i 's/File//g' /home/kanika/standard_minimizer_prosee/r

sed -i 's/MINvac.pdb//g' /home/kanika/standard_minimizer_prosee/r

我想将两个 sed 命令合并为一个,但我不知道方法。有人可以帮忙吗?

该文件如下所示:

-6174.27    File10MINvac.pdb
-514.451    File11MINvac.pdb
4065.68     File12MINvac.pdb
-4708.64    File13MINvac.pdb
6674.54     File14MINvac.pdb
8563.58     File15MINvac.pdb

I have a file r. I want to replace the words File and MINvac.pdb in it with nothing. The commands I used are

sed -i 's/File//g' /home/kanika/standard_minimizer_prosee/r

and

sed -i 's/MINvac.pdb//g' /home/kanika/standard_minimizer_prosee/r

I want to combine both sed commands into one, but I don't know the way. Can anyone help?

The file looks like this:

-6174.27    File10MINvac.pdb
-514.451    File11MINvac.pdb
4065.68     File12MINvac.pdb
-4708.64    File13MINvac.pdb
6674.54     File14MINvac.pdb
8563.58     File15MINvac.pdb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

揽清风入怀 2024-12-15 09:32:44

sed 是一种脚本语言。您可以使用分号或换行符分隔命令。许多 sed 方言还允许您将每个命令作为单独的 -e 选项参数传递。

sed -i 's/File//g;s/MINvac\.pdb//g' /home/kanika/standard_minimizer_prosee/r

我还添加了一个反斜杠来正确引用 pdb 之前的文字点,但在这个有限的上下文中,这可能并不重要。

为了完整起见,这里是换行变体。许多新手对 shell 允许在带引号的字符串中出现文字换行感到困惑,但这很方便。

sed -i 's/File//g
    s/MINvac\.pdb//g' /home/kanika/standard_minimizer_prosee/r

当然,在这种有限的情况下,您也可以将所有内容组合到一个正则表达式中:(

sed -i 's/\(File\|MINvac\.pdb\)//g' /home/kanika/standard_minimizer_prosee/r

某些 sed 方言希望不使用反斜杠,和/或提供使用扩展正则表达式的选项,它们应该是BSD sed 以及 MacOS sed 都需要 sed -i 的强制参数,但该参数可以为空,如 sed-i ''.)

sed is a scripting language. You separate commands with semicolon or newline. Many sed dialects also allow you to pass each command as a separate -e option argument.

sed -i 's/File//g;s/MINvac\.pdb//g' /home/kanika/standard_minimizer_prosee/r

I also added a backslash to properly quote the literal dot before pdb, but in this limited context that is probably unimportant.

For completeness, here is the newline variant. Many newcomers are baffled that the shell allows literal newlines in quoted strings, but it can be convenient.

sed -i 's/File//g
    s/MINvac\.pdb//g' /home/kanika/standard_minimizer_prosee/r

Of course, in this limited case, you could also combine everything into one regex:

sed -i 's/\(File\|MINvac\.pdb\)//g' /home/kanika/standard_minimizer_prosee/r

(Some sed dialects will want this without backslashes, and/or offer an option to use extended regular expressions, where they should be omitted. BSD sed, and thus also MacOS sed, demands a mandatory argument to sed -i which can however be empty, like sed -i ''.)

微暖i 2024-12-15 09:32:44

使用 -e 标志:

sed -i -e 's/File//g' -e 's/MINvac.pdb//g' /home/kanika/standard_minimizer_prosee/r

一旦您获得的命令多于使用 -e 方便定义的命令,最好将这些命令存储在单独的文件中并将其包含在-f 标志。

在这种情况下,您将创建一个包含以下内容的文件:

s/File//g
s/MINvac.pdb//g

让我们将该文件称为“sedcommands”。然后,您可以将其与 sed 一起使用,如下所示:

sed -i -f sedcommands /home/kanika/standard_minimizer_prosee/r

只有两个命令,可能不值得使用单独的命令文件,但如果需要进行大量转换,则非常方便。

Use the -e flag:

sed -i -e 's/File//g' -e 's/MINvac.pdb//g' /home/kanika/standard_minimizer_prosee/r

Once you get more commands than are convenient to define with -es, it is better to store the commands in a separate file and include it with the -f flag.

In this case, you'd make a file containing:

s/File//g
s/MINvac.pdb//g

Let's call that file 'sedcommands'. You'd then use it with sed like this:

sed -i -f sedcommands /home/kanika/standard_minimizer_prosee/r

With only two commands, it's probably not worthwhile using a separate file of commands, but it is quite convenient if you have a lot of transformations to make.

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