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

Google 自訂搜尋

Goole 廣告

隨機相片
Dell_V1320_00016.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2017/1/21 14:08:37
對於 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
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

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