3
(Get-Content C:\Users\georgeji\Desktop\KAI\KAI_Block_2\Temp\KAI_ORDER_DATARECON3.NONPUBLISH) | Foreach-Object {$_ -replace "~", "\034"} | Set-Content C:\Users\georgeji\Desktop\KAI\KAI_Block_2\Temp\KAI_ORDER_DATARECON4.NONPUBLISH

I am using the following command to replace ~ in text file with Field Seperator. This command run sucessfully but when i open the output file in notepadd ++. I am just seeing the plain \034.

Ex:

Company_Identifier\034Primary_Transaction_ID

But the output should be like below

enter image description here

Please Help

TomG
  • 281
  • 1
  • 2
  • 20

2 Answers2

3

Use

-replace "~", [char]0x1C

If you want to use it inside a longer string, you may use

-replace "~", "more $([char]0x1C) text"

The point here is that, in Powershell, you cannot use an octal char representation (nor \xYY or \uXXXX) since the escape sequences that it supports is limited to (see source)

   `0  Null
   `a  Alert bell/beep
   `b  Backspace
   `f  Form feed (use with printer output)
   `n  New line
   `r  Carriage return
 `r`n  Carriage return + New line
   `t  Horizontal tab
   `v  Vertical tab (use with printer output)

The `r (carriage return) is ignored in PowerShell (ISE) Integrated Scripting Environment host application console, it does work in a PowerShell console session.

Using the Escape character to avoid special meaning.

   ``  To avoid using a Grave-accent as the escape character
   `#  To avoid using # to create a comment
   `'  To avoid using ' to delimit a string
   `"  To avoid using " to delimit a string
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

Windows PowerShell does not have currently (see below) have escape sequences for character literals like \034.

Instead, what you can do is cast the numerical ascii or unicode value to [char] in a subexpression:

"Company_Identifier$([char]0x1C)Primary_Transaction_ID"

Similarly, you can provide the same cast expression as the right-most operand to -replace, it'll be converted to a single-character string:

... |Foreach-Object {$_ -replace "~", [char]0x1C} |...

PowerShell 6.0 (currently in beta) introduces the `u{} escape sequence for unicode codepoint literals:

... |Foreach-Object {$_ -replace "~", "`u{1C}"} |...

unicode literals in PowerShell 6.0.0-beta.7

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206