Michele
Michele

Reputation: 3881

Powershell replace command not removing newline

I have text that prints out like this:

mdbAppText_Arr: [0]: The  cover is open. {goes to next line here}
Please close the cover. and [1] Backprinter cover open 
46

I tried getting rid of the newline after open., and it's still there. Any idea of a better way or fix for what I'm doing? I need to get rid of the newline because it's going to a csv file, and messing up formatting (going to newline there).

This is my code:

$mdbAppText_Arr = $mdbAppText.Split("|")                        
$mdbAppText_Arr[0].replace("`r",";").replace("`n",";").replace("`t",";").replace("&",";") 
#replace newline/carriage return/tab with semicolon
if($alarmIdDef -eq "12-7")
{
     Write-Host "mdbAppText_Arr: [0]: $($mdbAppText_Arr[0]) and [1] $($mdbAppText_Arr[1]) "
     [byte] $mdbAppText_Arr[0][31]
}

I've been looking at:

replace

replace - this one has a link reference to lookup in the asci table, but it's unclear to me what column the byte equivalent is in the table/link.

I'm using PowerShell 5.1.

Upvotes: 0

Views: 93

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175085

-replace is a regex operator, so you need to supply a valid regular expression pattern as the right-hand side operand.

You can replace most newline sequences with a pattern describing a substring consisting of:

  • an optional carriage return (\r? in regex), followed by
  • a (non-optional) newline character (\n in regex):
$mdbAppText_Arr = $mdbAppText_Arr -replace '\r?\n'

Upvotes: 4

Related Questions