powershell - 如何使用 PowerShell 和 System.IO.StreamWriter 编写 Linux 换行符

标签 powershell newline

我需要使用 System.IO.StreamWriter 从 PowerShell 编写 Linux 风格的换行符(出于性能原因):

$writer = New-Object System.IO.StreamWriter "path\to\file"
$writer.WriteLine("{0};{1}", $timestamp, $value)

我接近解决方案:System.IO.StreamWriter 有一个 NewLine 属性,但它没有按预期工作:

$writer.NewLine = "\n"
$writer.WriteLine("{0};{1}", "23/12/2014", 45000)
$writer.WriteLine("{0};{1}", "24/12/2014", 54000)

产生:

23/12/2014;45000\n24/12/2014;54000\n...

最佳答案

在 PowerShell 双引号字符串中,使用反引号作为转义字符(不是反斜杠)。

尝试

$writer.NewLine = "`n"

关于powershell - 如何使用 PowerShell 和 System.IO.StreamWriter 编写 Linux 换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26801975/

相关文章:

c# - 用于删除旧文件的 Powershell 脚本

azure - Connect-AzureAD -确认抛出 AADSTS900144 : The request body must contain the following parameter: 'code' . 错误

Java BufferedWriter - newLine() 方法

git - 线尾: Git merge creates duplicates without conflict

c - 如何检测 scanf() 末尾的空格或换行符?

python - 正则表达式:不要将以换行符 (\n) 结尾的字符串与行尾 anchor ($) 匹配

powershell - 如何在Powershell中订阅 “user enter command to prompt”事件?

c# - 如何编写自定义 powershell 主机

python - 使用 cmd.exe 或 PowerShell 或 Python 从 Windows 命名管道读取

newline - Antd表格如何将文本分几行放入单元格