使用不同的分隔符和 Sed 删除 [英] Delete using a different delimiter with Sed
问题描述
相当肯定我遗漏了一些明显的东西!
Fairly certain I am missing something obvious!
$ cat test.txt
00 30 * * * /opt/timebomb.sh >> timebomb.log
01 30 * * * /opt/reincarnate.sh >> reincarnation.log
$ sed ':timebomb:d' test.txt
00 30 * * * /opt/timebomb.sh >> timebomb.log
01 30 * * * /opt/reincarnate.sh >> reincarnation.log
而
$ sed '/timebomb/d' test.txt
01 30 * * * /opt/reincarnate.sh >> reincarnation.log
为什么会这样?d
命令不支持一组不同的分隔符吗?
Why is it the case? Aren't a different set of delimiters supported for the d
command?
推荐答案
您使用的分隔符 //
不是用于 d
命令的,它们是为寻址.我认为您将它们与 s///
命令中的斜杠进行了比较……然而,尽管两者都与正则表达式有关,但它们是不同的上下文.
The delimiters //
that you're using are not for the d
command, they're for the addressing. I think you're comparing them to the slashes in the s///
command... However although both relate to regular expressions, they are different contexts.
地址(出现在命令之前)过滤了命令应用于哪些行...选项(出现在命令之后)用于控制应用替换.
The address (which appears before the command) filters which lines the command is applied to... The options (which appear after the command) are used to control the replacement applied.
如果要在地址中为正则表达式使用不同的分隔符,则需要以反斜杠开头地址:
If you want to use different delimiters for a regular expression in the address, you need to start the address with a backslash:
$ sed ':timebomb:d' test.txt
01 30 * * * /opt/reincarnate.sh >> reincarnation.log
(要了解地址和选项之间的区别,请考虑命令的输出:
(To understand the difference between the address and the options, consider the output of the command:
$ sed '/timebomb/s/log/txt/' test.txt
地址只选择包含正则表达式timebomb
的行,但选项告诉s
命令替换正则表达式log
和 txt
.)
The address chooses only the line(s) containing the regular expression timebomb
, but the options tell the s
command to replace the regular expression log
with txt
.)
这篇关于使用不同的分隔符和 Sed 删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!