组合两个 sed 命令
我有一个文件 r
.我想将其中的单词 File
和 MINvac.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sed
是一种脚本语言。您可以使用分号或换行符分隔命令。许多 sed 方言还允许您将每个命令作为单独的 -e 选项参数传递。我还添加了一个反斜杠来正确引用
pdb
之前的文字点,但在这个有限的上下文中,这可能并不重要。为了完整起见,这里是换行变体。许多新手对 shell 允许在带引号的字符串中出现文字换行感到困惑,但这很方便。
当然,在这种有限的情况下,您也可以将所有内容组合到一个正则表达式中:(
某些 sed 方言希望不使用反斜杠,和/或提供使用扩展正则表达式的选项,它们应该是BSD
sed
以及 MacOSsed
都需要sed -i
的强制参数,但该参数可以为空,如sed-i ''.
)sed
is a scripting language. You separate commands with semicolon or newline. Manysed
dialects also allow you to pass each command as a separate-e
option argument.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.
Of course, in this limited case, you could also combine everything into one regex:
(Some
sed
dialects will want this without backslashes, and/or offer an option to use extended regular expressions, where they should be omitted. BSDsed
, and thus also MacOSsed
, demands a mandatory argument tosed -i
which can however be empty, likesed -i ''.
)使用
-e
标志:一旦您获得的命令多于使用
-e
方便定义的命令,最好将这些命令存储在单独的文件中并将其包含在-f
标志。在这种情况下,您将创建一个包含以下内容的文件:
让我们将该文件称为“sedcommands”。然后,您可以将其与 sed 一起使用,如下所示:
只有两个命令,可能不值得使用单独的命令文件,但如果需要进行大量转换,则非常方便。
Use the
-e
flag:Once you get more commands than are convenient to define with
-e
s, 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:
Let's call that file 'sedcommands'. You'd then use it with sed like this:
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.