對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2017/1/21 14:08:37
對於 UNIX 和 DOS 有些認識的工程師應該都知道,UNIX 的換行是「0A」而我們怪怪的 DOS(Windows)則是「0D0A」!
而冷日近日就碰上了一個麻煩的狀況,就是『原本是 UNIX Format 的檔案經過 PowerShell 處理後就變成了 DOS Format』了!!!
所以冷日只好在 PowerShell 後多處理一次再把輸出檔案轉換回 UNIX Format!!!
網路上提供的方法大多如下:
或是
簡單來說,他們就是把 0D0A 換成 0A(\r\n -> \n)!
聽請來很合理對吧!?可不知道為何冷日怎麼試就是不 OK!
結果冷日最後的解法如下:
雖然看起來很怪(冷日也還沒搞懂那個 Join 是啥)但經實驗卻有效!?
在這裡分享給大家參考參考!
更歡迎有哪位大大可以告訴冷日到底是怎麼回事!?冷日會很感激的!
參考資料:
powershell - Unix newlines to windows newlines (on Windows) - Stack Overflow
windows - UNIX format files with Powershell - Stack Overflow
set-eol.ps1 CR/LF | PowerShell | SS64.com
Dos2Unix Text file format converters · PowerShell/Win32-OpenSSH Wiki · GitHub
Picus Pickings: Out-Unix : A function to output a Unix text file from PowerShell
powershell - how to convert a file from DOS to Unix - Stack Overflow
Get Content - Microsoft Developer Network
而冷日近日就碰上了一個麻煩的狀況,就是『原本是 UNIX Format 的檔案經過 PowerShell 處理後就變成了 DOS Format』了!!!

所以冷日只好在 PowerShell 後多處理一次再把輸出檔案轉換回 UNIX Format!!!
網路上提供的方法大多如下:
Get-Content in.csv -raw | % {$_ -replace "`r", ""} | Set-Content -NoNewline out.csv
或是
Get-ChildItem -File -Recurse | % { $x = get-content -raw -path $_.fullname; $x -replace "`r`n","`n" | set-content -path $_.fullname }
簡單來說,他們就是把 0D0A 換成 0A(\r\n -> \n)!
聽請來很合理對吧!?可不知道為何冷日怎麼試就是不 OK!
結果冷日最後的解法如下:
powershell -Command "(gc -Encoding UTF8 E:\Source\theFile2Change_DOS.csv) -join \"`n\" | Out-File -Encoding UTF8 E:\Source\theFile2Change.csv"
雖然看起來很怪(冷日也還沒搞懂那個 Join 是啥)但經實驗卻有效!?

在這裡分享給大家參考參考!
更歡迎有哪位大大可以告訴冷日到底是怎麼回事!?冷日會很感激的!

參考資料:
powershell - Unix newlines to windows newlines (on Windows) - Stack Overflow
windows - UNIX format files with Powershell - Stack Overflow
set-eol.ps1 CR/LF | PowerShell | SS64.com
Dos2Unix Text file format converters · PowerShell/Win32-OpenSSH Wiki · GitHub
Picus Pickings: Out-Unix : A function to output a Unix text file from PowerShell
powershell - how to convert a file from DOS to Unix - Stack Overflow
Get Content - Microsoft Developer Network