Given test.txt containing:
鉴于test.txt包含:
test
message
I want to end up with:
我想最终得到:
testing
a message
I think the following should work, but it doesn't:
我认为以下内容应该有效,但它不会:
Get-Content test.txt |% {$_-replace "t`r`n", "ting`r`na "}
How can I do a find and replace where what I'm finding contains CRLF?
我怎样才能找到并替换我所发现的包含CRLF的地方?
4 个解决方案
#1
31
A CRLF is two characters, of course, the CR and the LF. However, `n
consists of both. For example:
CRLF是两个字符,当然还有CR和LF。但是,`n由两者组成。例如:
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:\>
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.
我认为你遇到了`r的问题。我能够从你的例子中删除`r,只使用`n,并且它有效。当然,我不确切知道你是如何生成原始字符串所以我不知道那里有什么。
#2
16
In my understanding, Get-Content eliminates ALL newlines/carriage returns when it rolls your text file through the pipeline. To do multiline regexes, you have to re-combine your string array into one giant string. I do something like:
根据我的理解,Get-Content在通过管道滚动文本文件时会消除所有换行/回车。要执行多行正则表达式,您必须将字符串数组重新组合成一个巨大的字符串。我做的事情如下:
$text = [string]::Join("`n", (Get-Content test.txt))
[regex]::Replace($text, "t`n", "ting`na ", "Singleline")
Clarification: small files only folks! Please don't try this on your 40 GB log file :)
澄清:小文件只有人!请不要在40 GB的日志文件中尝试这个:)
#3
15
With -Raw
you should get what you expect
随着-Raw你应该得到你期望的
#4
0
You can use "\\r\\n"
also for the new line in powershell
. I have used this in servicenow tool.
您也可以将“\\ r \\ n”用于powershell中的新行。我在servicenow工具中使用过它。
In my case "\r\n"
s not working so i tried "\\r\\n"
as "\"
this symbol work as escape character in powershell
.
在我的情况下“\ r \ n”s不起作用,所以我尝试“\\ r \\ n”作为“\”这个符号在powershell中作为转义字符。
#1
31
A CRLF is two characters, of course, the CR and the LF. However, `n
consists of both. For example:
CRLF是两个字符,当然还有CR和LF。但是,`n由两者组成。例如:
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:\>
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.
我认为你遇到了`r的问题。我能够从你的例子中删除`r,只使用`n,并且它有效。当然,我不确切知道你是如何生成原始字符串所以我不知道那里有什么。
#2
16
In my understanding, Get-Content eliminates ALL newlines/carriage returns when it rolls your text file through the pipeline. To do multiline regexes, you have to re-combine your string array into one giant string. I do something like:
根据我的理解,Get-Content在通过管道滚动文本文件时会消除所有换行/回车。要执行多行正则表达式,您必须将字符串数组重新组合成一个巨大的字符串。我做的事情如下:
$text = [string]::Join("`n", (Get-Content test.txt))
[regex]::Replace($text, "t`n", "ting`na ", "Singleline")
Clarification: small files only folks! Please don't try this on your 40 GB log file :)
澄清:小文件只有人!请不要在40 GB的日志文件中尝试这个:)
#3
15
With -Raw
you should get what you expect
随着-Raw你应该得到你期望的
#4
0
You can use "\\r\\n"
also for the new line in powershell
. I have used this in servicenow tool.
您也可以将“\\ r \\ n”用于powershell中的新行。我在servicenow工具中使用过它。
In my case "\r\n"
s not working so i tried "\\r\\n"
as "\"
this symbol work as escape character in powershell
.
在我的情况下“\ r \ n”s不起作用,所以我尝试“\\ r \\ n”作为“\”这个符号在powershell中作为转义字符。