开发者

How can I Remove Special Formatting with PowerShell (Tabs, Cariage Enter, BackSpaces, Newlines, and more)

How can I Remove Special Formatting with PowerShell (Tabs, Cariage Enter, BackSpaces, Newlines, and more)

I want to remove these carriage returns (or at least I think that's what they are) using PowerShell b开发者_运维知识库ut I am having trouble doing so. I've used:

-replace "`n",""

-replace "`t",""

-replace "`b",""

-replace "`r",""

nothing seems to work. any suggestions?


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),""


try using regex:

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

or if values are numbers

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜