Sed和美元符号 [英] Sed and dollar sign
问题描述
Unix编程环境指出,在sed中的正则表达式中使用的'$'表示对我来说,这是对我来说很好的,因为
"The Unix Programming Environment" states that '$' used in regular expression in sed means end-of-the line which is fine for me, because
cat file | sed 's/$/\n/'
被解释为在最后添加换行符每行。
is interpretted as "add newline at the end of each line".
出现此问题,当我尝试使用命令:
The question arises, when I try to use the command:
cat file | sed '$d'
这行不应该删除每行而不是最后一行吗?在这种情况下,美元符号表示LAST行的末尾。我有什么问题?
Shouldn't this line remove each line instead of the last one? In this context, dollar sign means end of the LAST line. What am I getting wrong?
推荐答案
请注意,sed中的 regex
在分隔符内(;
,:
,〜
等等)
Note that regex
in sed must be inside the delimiters(;
,:
, ~
, etc) other than quotes.
/regex/
ex:
sed '/foo/s/bar/bux/g' file
或
~regex~
ex:
sed 's~dd~s~' file
而不是'regex'
。所以'$ d'
中的 $
不会被sed视为正则表达式。 '$ d'
的行为就像一个指出最后一行的地址。
but not 'regex'
. So $
in '$d'
won't be considered as regex by sed. '$d'
acts like an address which points out the last line.
这篇关于Sed和美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!