PowerShell Get-Content 讀檔換行陷阱

最近遇到一個 PowerShell 的陷阱,用 Get-Content 讀檔時,不會保留換行符號。因為 Get-Content 行為是單行回傳物件,多行回傳陣列,而不是單一字串。 一種簡單的 EOL 保留方法是

(Get-Content <path>) -join "`n"

# e.g.
$content = (Get-Content C:\Users\Me\note.txt) -join "`n"

把它串回來即可。 另一種作法是改用 .NET Class,但稍嫌麻煩。

[System.IO.File]::ReadAllText("file\to\path")

# e.g.
$content = [System.IO.File]::ReadAllText("C:\Users\Me\note.txt")

References

標籤 :

相關文章

LeetCode Problems: 7. Reverse Integer

題目資訊 名稱:Reverse Integer 分類:Algorithms 編號:7 難度:Easy 標籤:Math 網址:https://leetcode.com/

更多

在 Windows 上安裝並使用炫炮的終端機環境

繼在 Windows 上安裝 Oh My Posh 替換掉 PowerShell prompt 的樣式 這篇文章之後,我想提供我個人在 Windows 的 Terminal 開發環境,也順便用來記錄終端機環境如何安裝。 以下安裝說明僅支援 Windows 10 以上

更多

LeetCode Problems: 1. Two Sum

題目資訊 名稱:Two Sum 分類:Algorithms 編號:1 難度:Easy 標籤:Array, Hash Table 網址:https://leetcode.com/

更多