[英]regex in sed
I need to use sed to convert all occurences of ##XXX## to ${XXX}. 我需要使用sed将## XXX ##的所有出现转换为$ {XXX}。 X could be any alphabetic character or '_'.
X可以是任何字母字符或'_'。 I know that I need to use something like:
我知道我需要使用类似的东西:
's/##/\${/g'
But of course that won't properly, as it will convert ##FOO## to ${FOO${ 但当然不会正常,因为它会将## FOO ##转换为$ {FOO $ {
Any takers? 任何接受者? - Don
- 唐
Here's a shot at a better replacement regex: 这是一个更好的替代正则表达式的镜头:
's/##\([a-zA-Z_]\+\)##/${\1}/g'
Or if you assume exactly three characters : 或者,如果您假设正好三个字符:
's/##\([a-zA-Z_]\{3\}\)##/${\1}/g'
's/##\([a-zA-Z_]\+\)##/${\1}/g'
Use this: 用这个:
s/##\([^#]*\)##/${\1}/
BTW, there is no need to escape $ in the right side of the "s" operator. 顺便说一句,没有必要在“s”运算符的右侧逃避$。
sed 's/##\([a-zA-Z_][a-zA-Z_][a-zA-Z_]\)##/${\1}/'
The \\(...\\)
remembers...and is referenced as \\1
in the expansion. \\(...\\)
记住...并在扩展中被引用为\\1
。 Use single quotes to save your sanity. 使用单引号来保存您的理智。
As noted in the comments below this, this can also be contracted to: 如下面的评论中所述,这也可以签约:
sed 's/##\([a-zA-Z_]\{3\}\)##/${\1}/'
This answer assumes that the example wanted exactly three characters matched. 这个答案假定该示例只需要匹配三个字符。 There are multiple variations depending on what is in between the hash marks.
根据散列标记之间的内容,有多种变体。 The key part is remembering part of the matched string.
关键部分是记住匹配字符串的一部分。
echo "##foo##" | sed 's/##/${/;s//}/'
s
change only 1 occurence by default s
默认情况下仅更改1次出现 s//
take last search pattern used so second s take also ##
and only the second occurence still exist s//
采用最后使用的搜索模式,所以第二个也采用##
,只有第二次出现仍然存在 sed's /([^ az] [^ AZ] [^ 0-9] *)/(&)/ pg
echo '##XXX##' | echo'## XXX ##'| sed "s/^##([^#]*)/##${\\1}/g"
sed“s / ^ ##([^#] *)/ ## $ {\\ 1} / g”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.