使用Backticks逃避SED中的角色,而Windows上的Powershell运行

发布于 2025-01-23 02:29:28 字数 732 浏览 0 评论 0 原文

我知道,在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(`)替换所有后斜切(\)时,我似乎无法获得您可以看到的预期输出:

​将不胜感激。提前致谢。

I'm aware that, in PowerShell, backticks(`) are used to escape characters instead of the backslash(\). Hence, when using sed, this:

echo '"where am I"' | sed "s/\"[^\"]*\"/\033[96m&\033[0m/g;"

will not work and I would have to write it like this:

echo '"where am I"' | sed "s/`"[^\`"]*`"/`e[96m&`e[0m/g;"

Hence, I would like to do the same with this code:

echo '[where am I]' | sed "s/\[\([^09]\)/\033[94m[\033[0m\1/g;"

The expected output should be:

enter image description here

However, when I tried to replace all the backslashes(\) with backticks(`), I can't seem to get the expected output as you can see below:

enter image description here

Any help would be appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2025-01-30 02:29:28

据我所知, gnu sed 不支持八分之一逃脱序列 - 例如 \ 033 表示ESC char。 - 只有十六进制 - 例如 \ x1b

以下命令,使用十六进制。因此,逃生序列应在posix兼容的外壳和powershell中起作用:

# Note: Requires *GNU* sed
(echo '[where am I]' | sed 's/\[\([^09]\)/\x1b[94m[\x1b[0m\1/g')

换句话说:

  • 因为整个verbatim string( '...'...' 本身,不需要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)而不是) :

# Note: `e only works in PowerShell (Core) 7+
#       In Windows PowerShell, use $([char] 0x1b)
(echo '[where am I]' | sed "s/\[\([^09]\)/`e[94m[`e[0m\1/g")

重要:以上命令不包含嵌入式字符,它避免了 长期存在的powerShell bug 仍然存在于PowerShell(核心) )7.2.2:

  • 需要无条件,手动 \ -escape 嵌入式 ”字符。


  • 。逐字打印的3英寸:

     ##至少powershell 7.2.2:
    #不逃脱嵌入的“ \(也), 
    #实际上,它们将从论点中删除。
    #请注意,管道输入是 *不影响。
    
    '3“ Snow'| Findstr'\”'#Windows
    /bin/echo'3 \'snow'#unix
    
    #在可扩展的(双引号)字符串中,您必须逃脱 *两次 *:
    #一次用于PowerShell,然后进行外部程序呼叫。
    '3“ Snow'| Findstr” \`“”#Windows
    /bin/echo“ 3 \`snow”#unix
     

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:

# Note: Requires *GNU* sed
(echo '[where am I]' | sed 's/\[\([^09]\)/\x1b[94m[\x1b[0m\1/g')

In other words:

  • Because the entire, verbatim string ('...') is interpreted by sed 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 the Write-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):

# Note: `e only works in PowerShell (Core) 7+
#       In Windows PowerShell, use $([char] 0x1b)
(echo '[where am I]' | sed "s/\[\([^09]\)/`e[94m[`e[0m\1/g")

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:

    # Up to at least PowerShell 7.2.2:
    # Without escaping the embedded " with \ (too), 
    # they would in effect be removed from the arguments.
    # Note that pipeline input is *not* affected.
    
    '3" of snow' | findstr '\"'  # Windows
    /bin/echo '3\" of snow'      # Unix
    
    # In expandable (double-quoted) strings, you therefore must escape *twice*:
    # once for PowerShell, then for the external-program call.
    '3" of snow' | findstr "\`""  # Windows
    /bin/echo "3\`" of snow"      # Unix
    
太阳哥哥 2025-01-30 02:29:28

您可以使用单个引号将非相互插入的表达字符串传递给SED,';例如,'一个非交互字符串'。而不是围绕文本的双引号。 SED将采取通常的输入所做的事情。当您使用双引号时,Powershell逃脱了。

无论如何,SED的替代方案是利用PowerShell内置的正则表达式替代。例如,

Write-Output ('[where am i]' -replace '\[(.*?)\]', "<<`e[5;36m`$1`e[0m>>")

将捕获方括号之间的文本,并将其放在“人字形”之间,并使其眨眼。

这是一篇文章关于使用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,

Write-Output ('[where am i]' -replace '\[(.*?)\]', "<<`e[5;36m`$1`e[0m>>")

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文