Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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 ' '
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading