如何使用 PowerShell 替换换行符? [英] How can I replace newlines using PowerShell?

查看:161
本文介绍了如何使用 PowerShell 替换换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 test.txt 包含:

test
message

我想结束:

testing
a message

我认为以下应该有效,但它没有:

I think the following should work, but it doesn't:

Get-Content test.txt |% {$_-replace "t`r`n", "ting`r`na "}

如何查找并替换我发现的包含 CRLF 的内容?

How can I do a find and replace where what I'm finding contains CRLF?

推荐答案

CRLF 是两个字符,当然是 CR 和 LF.但是,`n 包含两者.例如:

A CRLF is two characters, of course, the CR and the LF. However, `n consists of both. For example:

PS C:\> $x = "Hello
>> World"

PS C:\> $x
Hello
World
PS C:\> $x.contains("`n")
True
PS C:\> $x.contains("`r")
False
PS C:\> $x.replace("o`nW","o There`nThe W")
Hello There
The World
PS C:\>

我认为您遇到了 `r 的问题.我能够从您的示例中删除 `r,仅使用 `n,并且它起作用了.当然,我不知道你是如何生成原始字符串的,所以我不知道里面有什么.

I think you're running into problems with the `r. I was able to remove the `r from your example, use only `n, and it worked. Of course, I don't know exactly how you generated the original string so I don't know what's in there.

这篇关于如何使用 PowerShell 替换换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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