sed、逗号、美元符号、单引号和双引号 [英] sed, comma, dollar sign, single quote and double quote
问题描述
这个问题很复杂,但是我希望答案会很简单,只是因为我忽略了一些东西.
The question is complex, but I hope the answer will be simple and just because I've overlook something.
我想做的是,我试图使用"sed"命令从文件中获取匹配行之后的所有行.
What I'm trying to do is, I'm trying to use "sed" command to get all lines since a matched line, from a file.
我发现以下命令可用于我的目的:
I figure out the following commands works for my purpose:
sed -n '/\[ ocsp \]/,$p' openssl.cnf
但是,我想将匹配的单词作为变量传入.因此,我需要将报价从单引号更改为双引号.
However, I'd like to have the matching word passed in as a variable. Therefore, I'll need to change the quote from single quote to double quote.
它没有按我预期的那样工作!
It DOES NOT WORK as I expected!
我的理解是,逗号和美元符号告诉sed命令从匹配点打印到文件末尾.我在这里没看到什么好笑的.但是,当我更改为双引号后,会收到如下错误消息:
My understanding is that, the comma and dollar sign tell s the sed command to print from the matched point until the end of file. I didn't see anything funny here. However, after I change to double quote, I get error message like:
sed: -e expression #1, char 19: unexpected `,'
,我尝试将模式匹配到{}符号等后进行转义或放置命令.没有任何效果.
and I tried escape or put the command after matching pattern in to {} sign etc. Nothing works.
请在这里需要一些帮助.
Need some help out here, please.
推荐答案
内部双引号bash将 $ var
扩展为 var
的值.bash内的单引号不会以这种方式解释美元符号.因此,如果您更改引号,则 $ p
会扩展为(可能是)空值并产生错误.只需在最后一个 $
之前添加单个baskslash.
Inside double quotes bash expands $var
to the value of var
. Inside single quotes bash doesn't interpret a dollar sign in this way. So if you changed quotes, than $p
was expanded to (probably) empty value and yelded an error. Just add single baskslash before last $
.
sed -n "/\\[ ocsp \\]/,\$p" openssl.cnf
这篇关于sed、逗号、美元符号、单引号和双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!