使用Backticks逃避SED中的角色,而Windows上的Powershell运行
我知道,在PowerShell中,Backticks(`)用于逃避角色,而不是反斜击(\)。因此,当使用 sed
>时,这
echo '"where am I"' | sed "s/\"[^\"]*\"/\033[96m&\033[0m/g;"
将是不起作用的,我必须这样写:
echo '"where am I"' | sed "s/`"[^\`"]*`"/`e[96m&`e[0m/g;"
因此,我想对此代码进行相同的操作:
echo '[where am I]' | sed "s/\[\([^09]\)/\033[94m[\033[0m\1/g;"
预期的输出应为:
但是,当我尝试用Backticks(`)替换所有后斜切(\)时,我似乎无法获得您可以看到的预期输出:
将不胜感激。提前致谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知, gnu
sed
不支持八分之一逃脱序列 - 例如\ 033
表示ESC char。 - 只有十六进制 - 例如\ x1b
。以下命令,使用十六进制。因此,逃生序列应在posix兼容的外壳和powershell中起作用:
换句话说:
'...'...'
” ...“
)。注意:
封闭式
(...)
,Windows PowerShell在Windows上正确终端(控制台)中的VT(虚拟终端)序列 - 请参见此答案。如果您以变量或重定向捕获输出,则不需要。在powerShell中,使用
echo
-写入输出
cmdlet-不是必需的,并且由于PowerShell的 in em> intimit 输出行为。如果您的
sed
实现不支持逃脱序列,例如\ x1b
- 例如MacOS--您确实必须诉诸于使用 PowerShell的 在“ ...” );也就是说,您必须将\ x1b
替换为Escape sequence`e
才能将a verbatim enc ecc cartar em Front (在不支持`e
的Windows PowerShell 中,使用subexpression$([char] 0x1b)
而不是) :重要:以上命令不包含嵌入式
“
字符,它避免了 长期存在的powerShell bug 仍然存在于PowerShell(核心) )7.2.2:需要无条件,手动
\
-escape 嵌入式“
”字符。As far as I'm aware, GNU
sed
doesn't support octal escape sequences - such as\033
to represent an ESC char. - only hexadecimal ones - such as\x1b
.The following command, which uses hex. escape sequences, should therefore work in both POSIX-compatible shells and in PowerShell:
In other words:
'...'
) is interpreted bysed
itself, there is no need for PowerShell's string interpolation (via"..."
).Note:
The enclosing
(...)
, which on Windows is required for Windows PowerShell to properly render the ANSI escape codes / VT (Virtual Terminal) sequences in the terminal (console) - see this answer. It isn't needed if you capture the output in a variable or redirect it.In PowerShell, use of
echo
- a built in alias of theWrite-Output
cmdlet - isn't necessary and can be omitted, due to PowerShell's implicit output behavior.If your
sed
implementation doesn't support escape sequences such as\x1b
- such as on macOS - you must indeed resort to using PowerShell's escape sequences inside an expandable (double-quoted) string ("..."
); that is, you must replace\x1b
with escape sequence`e
in order to embed a verbatim ESC character into the string, up front (in Windows PowerShell, which doesn't support`e
, use subexpression$([char] 0x1b)
instead):Important: The above commands contain no embedded
"
characters, which avoids a long-standing PowerShell bug that is still present as of PowerShell (Core) 7.2.2:The need to unconditionally, manually
\
-escape embedded"
chars. in arguments passed to external programs - see this answer for details.A few quick examples that print verbatim
3" of snow
:您可以使用单个引号将非相互插入的表达字符串传递给SED,
'
;例如,'一个非交互字符串'
。而不是围绕文本的双引号。 SED将采取通常的输入所做的事情。当您使用双引号时,Powershell逃脱了。无论如何,SED的替代方案是利用PowerShell内置的正则表达式替代。例如,
将捕获方括号之间的文本,并将其放在“人字形”之间,并使其眨眼。
这是一篇文章关于使用powerShell在文本中插入ANSI的文章。
You can pass non-interpolated expression strings to sed by using single quotes,
'
; e.g.,'a non-interpolated string'
. instead of double quotes around the text. sed would take what it normally does for input. When you use double quotes, that's when powershell escaping comes into play.Anyway, an alternative to sed would be leveraging the regular expression substitution built into powershell. For instance,
Will capture the text between square brackets and place it between "chevrons" and make it blink.
Here's an article on inserting ANSI in text using powershell.