获取内容并显示控制字符,例如`r - 可视化字符串中的控制字符 [英] Get-Content and show control characters such as `r - visualize control characters in strings

查看:60
本文介绍了获取内容并显示控制字符,例如`r - 可视化字符串中的控制字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以向 Get-Content 传递什么标志来显示 控制字符\r\n\n ?

What flag can we pass to Get-Content to display control characters such as \r\n or \n?

我想要做的是确定文件的行尾是 Unix 还是 Dos 风格.我试过简单地运行 Get-Content,它不显示任何行结尾.我也尝试过使用带有 set list 的 Vim,它只显示 $,不管行尾是什么.

What I am trying to do, is to determine whether the line endings of a file are in the Unix or Dos style. I have tried simply running Get-Content, which doesn't show any line ending. I have also tried using Vim with set list, which just shows the $ no matter what the line ending is.

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

I would like to do this with PowerShell, because that would be mighty useful.

推荐答案

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

One way is to use Get-Content's -Encoding parameter e.g.:

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

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

If you have the PowerShell Community Extensions, you can use the Format-Hex command:

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\n"而不是按照 BaconBits 的建议去做,但你必须使用 -Raw 参数,例如:

If you really want to see "\r\n" in the output than do what BaconBits suggests but you have to use the -Raw parameter e.g.:

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

输出:

asf\tasdfasdf\tasdfas\r\nasdf\r\nas\tasd\r\nasdfasd\tasf\tasdf\t\r\nasdf

这篇关于获取内容并显示控制字符,例如`r - 可视化字符串中的控制字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆