0

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.

1 Answer 1

4

-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'
Sign up to request clarification or add additional context in comments.

3 Comments

Expanded to encompass removal of ampersands / tabs (as per OP initial sequence '-replace "&|\t|\r?\n",';''
@SagePourpre Looks like an answer waiting to be written :)
Thanks a lot! Works great! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.