结合两个 sed 命令 [英] Combining two sed commands
问题描述
我有一个文件r
.我想将其中的 File
和 MINvac.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
和
sed -i 's/MINvac.pdb//g' /home/kanika/standard_minimizer_prosee/r
我想将两个 sed
命令合二为一,但我不知道怎么做.有人可以帮忙吗?
I want to combine both sed
commands into one, but I don't know the way. Can anyone help?
文件如下所示:
-6174.27 File10MINvac.pdb
-514.451 File11MINvac.pdb
4065.68 File12MINvac.pdb
-4708.64 File13MINvac.pdb
6674.54 File14MINvac.pdb
8563.58 File15MINvac.pdb
推荐答案
sed
是一种脚本语言.用分号或换行符分隔命令.许多 sed
方言还允许您将每个命令作为单独的 -e
选项参数传递.
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
我还在pdb
之前添加了一个反斜杠来正确引用文字点,但在这个有限的上下文中这可能并不重要.
I also added a backslash to properly quote the literal dot before pdb
, but in this limited context that is probably unimportant.
为了完整起见,这里是换行符变体.许多新手对 shell 允许在带引号的字符串中使用文字换行符感到困惑,但它可以很方便.
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
(一些 sed
方言会希望这个没有反斜杠,和/或提供使用扩展正则表达式的选项,它们应该被省略.BSD sed
,因此也是 MacOS sed
,要求 sed -i
的强制参数,但可以为空,如 sed -i ''.
)
(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 ''.
)
这篇关于结合两个 sed 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!