Sha*_*tin 5 powershell diagnostics control-characters
我们可以传递什么标志Get-Content
来显示控制字符,如\r\n
或\n
?
我想要做的是确定文件的行结尾是Unix还是Dos风格.我试过简单的运行Get-Content
,没有显示任何行结束.我也尝试过使用Vim set list
,它只显示$
了行结尾的内容.
我想用PowerShell做这件事,因为那将是非常有用的.
一种方法是使用Get-Content的-Encoding参数,例如:
Get-Content foo.txt -Encoding byte | % {"0x{0:X2}" -f $_}
Run Code Online (Sandbox Code Playgroud)
如果您有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
Run Code Online (Sandbox Code Playgroud)
如果你真的想在输出中看到"\ r \n"而不是BaconBits建议但你必须使用-Raw参数,例如:
(Get-Content foo.txt -Raw) -replace '\r','\r' -replace '\n','\n' -replace '\t','\t'
Run Code Online (Sandbox Code Playgroud)
输出:
asf\tasdfasdf\tasdfas\r\nasdf\r\nas\tasd\r\nasdfasd\tasf\tasdf\t\r\nasdf
Run Code Online (Sandbox Code Playgroud)
以下是自定义函数Debug-String
,该函数可视化字符串中的控制字符:
在可用的情况下,使用PowerShell自己的`
前缀转义序列表示法(例如,`r
对于CR),其中可以使用本机PowerShell转义,
退回到插入符号(例如,代码点为0x4
END OF TRANSMISSION 的ASCII范围控制字符表示为^D
)。
-CaretNotation
开关以脱字符号表示所有 ASCII范围的控制字符,这使您的输出类似于cat -A
Linux和cat -et
macOS / BSD上的输出。所有其他控制字符,即ASCII范围之外的字符(跨越代码点的ASCII范围0x0
- 0x7F
)均以形式表示`u{<hex>}
,其中<hex>
十六进制。代码点的表示形式,最多6位;例如,`u{85}
是Unicode字符。U+0085
,NEXT LINE控制字符。现在,可扩展字符串("..."
)也支持此表示法,但仅在PowerShell Core中支持。
适用于你的使用情况,您最好使用(需要PSv3 +,由于使用Get-Content -Raw
以确保文件被读取作为一个整体,没有它,有关行尾的信息将会丢失):
Get-Content -Raw $file | Debug-String
Run Code Online (Sandbox Code Playgroud)
两个简单的例子:
使用PowerShell的转义序列表示法。请注意,这看起来像是没有操作的:“ ...”字符串中的`前缀序列会创建实际的控制字符。
PS> "a`ab`t c`0d`r`n" | Debug-String
Run Code Online (Sandbox Code Playgroud)
Get-Content -Raw $file | Debug-String
Run Code Online (Sandbox Code Playgroud)
使用-CaretNotation
,输出类似于cat -A
Linux:
PS> "a`ab`t c`0d`r`n" | Debug-String -CaretNotation
Run Code Online (Sandbox Code Playgroud)
PS> "a`ab`t c`0d`r`n" | Debug-String
Run Code Online (Sandbox Code Playgroud)
Debug-String
源代码: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
}
Run Code Online (Sandbox Code Playgroud)
为了简洁起见,我没有在上面包括基于注释的帮助;这里是:
a`ab`t c`0d`r`n
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6704 次 |
最近记录: |