如何使用 PowerShell 删除特殊格式(制表符、回车键、退格键、换行符等)

发布于 2024-12-06 16:26:19 字数 249 浏览 0 评论 0原文

在此处输入图像描述

我想使用 PowerShell 删除这些回车符(或者至少我认为它们是什么),但我我这样做有困难。我用过:

-替换“`n”,“”

-替换“`t”、“”

-替换“`b”、“”

-替换“`r”、“”

似乎没有任何效果。有什么建议吗?

enter image description here

I want to remove these carriage returns (or at least I think that's what they are) using PowerShell but I am having trouble doing so. I've used:

-replace "`n",""

-replace "`t",""

-replace "`b",""

-replace "`r",""

nothing seems to work. any suggestions?

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

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

发布评论

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

评论(2

记忆消瘦 2024-12-13 16:26:19

如果您想对删除进行手术,一种粗略但有效的方法是使用 .Net ASCII 编码对象来获取有问题的字符的数值:

$text = "Test`n`b`r"
$enc = New-Object System.Text.ASCIIEncoding
$text.ToCharArray() | Foreach-Object { echo "$($_) $($enc.GetBytes($_))" }

对于文本行中的每个字符,该代码将输出字符,然后输出其数值。它看起来像这样(括号中的字符不会出现在实际输出中,它们在那里是为了澄清):

T 84
e 101
s 115
t 116

 10 (`n)
 8  (`b)
 13 (`r)

通过针对 CSV 文件的一行运行它,您应该能够检测到需要删除的内容。然后,您可以通过将数值转换回其表示的 ASCII 字符来执行替换。例如,这两个语句都会从文本中删除“b”字符:

$text -replace "`b",""
$text -replace $enc.GetChars(8),""

If you want to be surgical about the deletions, one crude but effective method is to use a .Net ASCII encoding object to get a numeric value of the offending character(s):

$text = "Test`n`b`r"
$enc = New-Object System.Text.ASCIIEncoding
$text.ToCharArray() | Foreach-Object { echo "$($_) $($enc.GetBytes($_))" }

For every character in a line of your text, that code will output the character and then its numeric value. It will look something like this (characters in parentheses won't appear in the actual output, they're there for clarification):

T 84
e 101
s 115
t 116

 10 (`n)
 8  (`b)
 13 (`r)

By running it against one line of your CSV file, you should be able to detect what needs to be stripped. You can then perform a replacement by converting the numeric value back into the ASCII character it represents. For instance, both of these statements will delete a `b character from your text:

$text -replace "`b",""
$text -replace $enc.GetChars(8),""
我的鱼塘能养鲲 2024-12-13 16:26:19

尝试使用正则表达式:

[regex]::Replace("←xxxx←", "←" , "" ) # give xxxx

或者如果值是数字

[regex]::Replace("←356←", "\D" , "" ) # give 356

try using regex:

[regex]::Replace("←xxxx←", "←" , "" ) # give xxxx

or if values are numbers

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