I want to read a .dat file as binary stream in powershell. As I am new to Powershell, not able to figure out how it can be done. After file being read as a binary stream and would like to replace the binary CRLF characters with LF..
Asked
Active
Viewed 2,835 times
0
-
`Get-Content -Encoding Byte`? What do you want to do with the data after it was read (other than replacing line breaks)? – Ansgar Wiechers Mar 04 '19 at 13:25
-
I just want to replace line breaks and write data into a new file – TAdhav Mar 04 '19 at 13:46
-
```Set-Content .\newfile.dat -Value ((Get-Content $file) -join "`n")``` – Mathias R. Jessen Mar 04 '19 at 13:55
-
thanks Mathias. But data file have characters as below : These characters are not getting converted properly. test‡test → testx82test test,test → testx82test – TAdhav Mar 04 '19 at 14:05
-
1Specify the proper encoding. – Ansgar Wiechers Mar 04 '19 at 14:12
-
1@MathiasR.Jessen: Note that you also need `-NoNewline` (and a manually appended LF), otherwise `Set-Content` will append a trailing CRLF (on Windows). – mklement0 Mar 04 '19 at 14:16
-
@TAdhav: Note that Windows PowerShell defaults to "ANSI" encoding for both `Get-Content` and `Set-Content`. If your input file is UTF-8-encoded but has _no BOM_, for instance, it will be misinterpreted; use `-Encoding` to control the character encoding on both reading and writing. – mklement0 Mar 04 '19 at 15:12
1 Answers
0
You can use .Net $bytes = [System.IO.File]::ReadAllBytes("<YOUR FILE>")
or Powershell $bytes = Get-Content -Path "<YOUR FILE>" -Encoding Byte -Raw
to read the binary data as byte array (System.Byte[]
).
If you convert that to ASCII string with $text = [System.Text.Encoding]::ASCII.GetString($bytes)
you should be able to replace the CRLF to LF.
Something like this:
# read the binary data as byte array
$bytes = [System.IO.File]::ReadAllBytes("<YOUR FILE>")
# convert to ASCII string
$text = [System.Text.Encoding]::ASCII.GetString($bytes)
# replace the CRLF to LF
$text = $text -replace "`r`n", "`n"
# convert back to byte array
$bytes = [System.Text.Encoding]::ASCII.GetBytes($text)

Theo
- 57,719
- 8
- 24
- 41
-
this data file have charcters as below : These characters are not getting converted properly. test‡test → test????test test,test → test???test – TAdhav Mar 04 '19 at 13:52
-
The read-as-bytes techniques are useful, but only needed if you're dealing with a (partly) binary file or if you have no idea what (single-byte) character encoding the input file uses. If you do know the character encoding, just use `Get/Set-Content -Encoding`; if it's an exotic encoding that PowerShell doesn't expose but .NET does, use the `[System.IO.File]` text-file method overloads that accept an encoding object. – mklement0 Mar 04 '19 at 15:16