获取内容并显示控制字符,例如 'r语言 - 可视化字符串中的控制字符



我们可以传递给什么标志Get-Content来显示控制字符,例如rnn

我正在尝试做的是确定文件的行尾是Unix还是Dos样式。我尝试简单地运行Get-Content,它不显示任何行尾。我还尝试将 Vim 与 set list 一起使用,无论行尾是什么,它都只显示$

我想用PowerShell来做这件事,因为那将非常有用。

一种方法是使用 Get-Content 的 -Encoding 参数,例如:

Get-Content foo.txt -Encoding byte | % {"0x{0:X2}" -f $_}

如果你有 PowerShell 社区扩展,则可以使用 Format-Hex 命令:

Format-Hex foo.txt
Address:  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F ASCII
-------- ----------------------------------------------- ----------------
00000000 61 73 66 09 61 73 64 66 61 73 64 66 09 61 73 64 asf.asdfasdf.asd
00000010 66 61 73 0D 0A 61 73 64 66 0D 0A 61 73 09 61 73 fas..asdf..as.as

如果您确实想在输出中看到"\r",请执行培根比特的建议,但您必须使用 -Raw 参数,例如:

(Get-Content foo.txt -Raw) -replace 'r','r' -replace 'n','n' -replace 't','t'

输出:

asftasdfasdftasdfasrnasdfrnastasdrnasdfasdtasftasdftrnasdf

下面是自定义函数Debug-String,它可视化字符串中的控制字符

  • 如果可用,使用 PowerShell 自己的`前缀转义序列表示法(例如,`r 表示 CR(,其中本机 PowerShell 转义可用,

  • 回退到插入符号表示法(例如,带有代码点 0x4 - 传输结束 - 的 ASCII 范围控制字符表示为 ^D (。

    • 或者,您可以使用 -CaretNotation 开关以插入符号表示法表示所有 ASCII 范围控制字符,这为您提供类似于 Linux 上的 cat -A 和 macOS/BSD 上的cat -et输出。
  • 所有其他控制字符,即 ASCII 范围之外的字符(跨越码位的 ASCII 范围0x0 - 0x7F(以 `u{<hex>} 的形式表示,其中 <hex> 是最多 6 位数字的代码点的十六进制表示;例如,`u{85} 是 Unicode 字符。 U+0085,下一行控制字符;这种表示法现在在可扩展字符串("..."(中也受支持,但仅在PowerShell Core中受支持。

应用于您的用例,您将使用(需要使用 PSv3+,因为使用 Get-Content -Raw 来确保文件作为一个整体读取;没有它,有关行尾的信息将丢失(:

Get-Content -Raw $file | Debug-String

两个简单的例子:

<小时 />

使用 PowerShell 的转义序列表示法。请注意,这看起来只是无操作:"..."字符串创建实际控制字符。

PS> "a`ab`t c`0d`r`n" | Debug-String
a`ab`t c`0d`r`n

使用 -CaretNotation ,输出类似于 Linux 上的 cat -A

PS> "a`ab`t c`0d`r`n" | Debug-String -CaretNotation
a^Gb^I c^@d^M$
<小时 />

Debug-String源代码:

注意:以下功能也可作为 MIT 许可的 Gist 提供,具有附加功能,特别是将空格显示为·和将非 ASCII 字符显示为转义序列的选项 (-UnicodeEscapes (,以及将字符串打印为 PowerShell 字符串文本的选项 ( -AsSourceCode (。今后只会保留 Gist。

假设您已经查看了链接的代码以确保它是安全的(我个人可以向您保证,但您应该始终检查(,您可以直接安装它,如下所示:

irm https://gist.github.com/mklement0/7f2f1e13ac9c2afaf0a0906d08b392d1/raw/Debug-String.ps1 | iex
Function Debug-String {
  param(
    [Parameter(ValueFromPipeline, Mandatory)]
    [string] $String
    ,
    [switch] $CaretNotation
  )
  begin {
    # p{C} matches any Unicode control character, both inside and outside
    # the ASCII range; note that tabs (`t) are control character too, but not spaces.
    $re = [regex] 'p{C}'
  }
  process {
    $re.Replace($String, {
      param($match)
      $handled = $False
      if (-not $CaretNotation) {
        # Translate control chars. that have native PS escape sequences into them.
        $handled = $True
        switch ([Int16] [char] $match.Value) {
          0  { '`0'; break }
          7  { '`a'; break }
          8  { '`b'; break }
          12 { '`f'; break }
          10 { '`n'; break }
          13 { '`r'; break }
          9  { '`t'; break }
          11 { '`v'; break }
          default { $handled = $false }
        } # switch
      }
      if (-not $handled) {
          switch ([Int16] [char] $match.Value) {
            10 { '$'; break } # cat -A / cat -e visualizes LFs as '$'
            # If it's a control character in the ASCII range, 
            # use caret notation too (C0 range).
            # See https://en.wikipedia.org/wiki/Caret_notation
            { $_ -ge 0 -and $_ -le 31 -or $_ -eq 127 } {
              # Caret notation is based on the letter obtained by adding the
              # control-character code point to the code point of '@' (64).
              '^' + [char] (64 + $_)
              break
            }
            # NON-ASCII control characters; use the - PS Core-only - Unicode
            # escape-sequence notation:
            default { '`u{{{0}}}' -f ([int16] [char] $_).ToString('x') }
          }
      } # if (-not $handled)
    })  # .Replace
  } # process
}

为简洁起见,我没有包括上面基于注释的帮助;这里是:

<#
.SYNOPSIS
Outputs a string in diagnostic form.
.DESCRIPTION
Prints a string with normally hidden control characters visualized.
Common control characters are visualized using PowerShell's own escaping 
notation by default, such as
"`t" for a tab, "`n" for a LF, and "`r" for a CR.
Any other control characters in the ASCII range (C0 control characters)
are represented in caret notation (see https://en.wikipedia.org/wiki/Caret_notation).
If you want all ASCII range control characters visualized using caret notation,
except LF visualized as "$", similiar to `cat -A` on Linux, for instance, 
use -CaretNotation.
Non-ASCII control characters are visualized by their Unicode code point
in the form `u{<hex>}, where <hex> is the hex. representation of the
code point with up to 6 digits; e.g., `u{85} is U+0085, the NEXT LINE
control char.
.PARAMETER CaretNotation
Causes LF to be visualized as "$" and all other ASCII-range control characters
in caret notation, similar to `cat -A` on Linux.
.EXAMPLE
PS> "a`ab`t c`0d`r`n" | Debug-String
a`ab`t c`0d`r`n
.EXAMPLE
PS> "a`ab`t c`0d`r`n" | Debug-String -CaretNotation
a^Gb^I c^@d^M$
#>

以下是使用正则表达式替换的一种方法:

function Printable([string] $s) {
    $Matcher = 
    {  
      param($m) 
      $x = $m.Groups[0].Value
      $c = [int]($x.ToCharArray())[0]
      switch ($c)
      {
          9 { 't' }
          13 { 'r' }
          10 { 'n' }
          92 { '\' }
          Default { "$c" }
      }
    }
    return ([regex]'[^ -~\]').Replace($s, $Matcher)
}
PS C:> $a = [char[]](65,66,67, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
PS C:> $b = $a -join ""
PS C:> Printable $b
ABC12345678tn1112r

最新更新