英文:
Avoid Select-Object output on screen
问题
I am having a bit of trouble with Select-Object.
With the following piece of (truncated) code:
$totalData=(Get-Content $Env:TEMP\FBIUtemp2.txt)
.....
$data=""
....
....
$data =($totalData | Select-Object -Index ($s..$limit))
$data.Replace("`r`n"," ")
$data.Replace("`n"," ")
I get a String (list of String separated with newline termination) from a file and get lines between $s and $limit into another $data String.
The code work as expected except that Select-Object keeps showing result on the screen and I would like for it not to do that.
I tried multiple things, but it either ends up doing nothing or stopping the script. I tried to mute the result with Out-Null, but then it doesn't fill the $data variable. Is there a parameter that I am missing?
Also, I don't want to split the $totalData String in a list to reassemble it later, as it would slow down the whole process.
Thank you for your help.
英文:
I am having a bit of trouble with Select-Object.
With the following piece of (truncated) code:
$totalData=(Get-Content $Env:TEMP\FBIUtemp2.txt)
.....
$data=""
....
....
$data =($totalData | Select-Object -Index ($s..$limit))
$data.Replace("`r`n"," ")
$data.Replace("`n"," ")
I get a String (list of String separated with newline termination) from a file and get lines between $s and $limit into another $data String.
The code work as expected except that Select-Object keeps showing result on the screen and I would like for it not to do that.
I tried multiple things, but it either ends up doing nothing or stopping the script. I tried to mute the result with Out-Null, but then it doesn't fill the $data variable. Is there a parameter that I am missing?
Also, I don't want to split the $totalData String in a list to reassemble it later, as it would slow down the whole process.
Thank you for your help.
答案1
得分: 1
不是 Select-Object
产生可见输出,而是您的 $data.Replace("
rn"," ")
和 $data.Replace("
n"," ")` 方法调用,两者都会 输出 字符串替换的结果,而不是 原地修改 输入字符串(这在根本上不受支持,因为 .NET 字符串是 不可变 的)。
$data
包含一个 行数组,根据定义,其元素 不 包含嵌入的换行符(无论是
n
(LF)还是
rn
`(CRLF))。
如果您的意图是用空格连接这些行:
$data = $data -join ' '
英文:
<!-- language-all: sh -->
It isn't Select-Object
that produces visible output, it is your $data.Replace("`r`n"," ")
and $data.Replace("`n"," ")
method calls, both of which output the result of the string replacement - rather than modifying the input string(s) in place (something that is fundamentally unsupported, given that .NET strings are immutable).
$data
contains an array of lines, whose elements by definition do not have embedded newlines (whether in the form of `n
(LF) or `r`n
(CRLF)).
If your intent is to join those lines with spaces:
$data = $data -join ' '
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论