茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670614 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_0051.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

微軟帝國 : [原創]如何用 PowerShell 把 DOS File Format 轉換成 UNIX Format?

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[原創]如何用 PowerShell 把 DOS File Format 轉換成 UNIX Format?
對於 UNIX 和 DOS 有些認識的工程師應該都知道,UNIX 的換行是「0A」而我們怪怪的 DOS(Windows)則是「0D0A」!
而冷日近日就碰上了一個麻煩的狀況,就是『原本是 UNIX Format 的檔案經過 PowerShell 處理後就變成了 DOS Format』了!!! OMG

所以冷日只好在 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
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|