避免在屏幕上显示 Select-Object 输出。

huangapple go评论47阅读模式
英文:

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(&quot;`r`n&quot;,&quot; &quot;) and $data.Replace(&quot;`n&quot;,&quot; &quot;) 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 &#39; &#39;

huangapple
  • 本文由 发表于 2023年6月1日 02:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定