環境:Windows 10 pro 20H2,PowerShell 5.1.19041.1237
在.txt檔案中,我的以下PowerShell代碼沒有用" ". 問題:我在這里可能缺少什么,我們如何使它起作用?
C:\MyFolder\Test.txt檔案:
This is first line.
This is second line.
This is third line.
This is fourth line.
所需的輸出[用“”字符替換換行符后]:
This is first line. This is second line. This is third line. This is fourth line.
PowerShell 代碼:
PS C:\MyFolder\test.txt> $content = get-content "Test.txt"
PS C:\MyFolder\test.txt> $content = $content.replace("`r`n", " ")
PS C:\MyFolder\test.txt> $content | out-file "Test.txt"
評論
如果我替換檔案中的其他一些字符,上面的代碼作業正常。例如,如果更改與上述代碼的第二行$content = $content.replace("third", "3rd")中,代碼成功地替換third與3rd在上述檔案中。
uj5u.com熱心網友回復:
您需要將-Raw引數傳遞給Get-Content. 默認情況下,沒有Raw引數,內容作為換行符分隔的字串陣列回傳。
獲取內容“Test.txt” -Raw
從檔案中參考,
-原料
忽略換行符并在保留換行符的一個字串中回傳檔案的全部內容。默認情況下,檔案中的換行符用作分隔符以將輸入分隔為字串陣列。此引數是在 PowerShell 3.0 中引入的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333056.html
