powershell - 逐行处理HereString

标签 powershell herestring

我想定义一个HereString然后逐行使用它。最初,我认为这些行是在HereString中固有的,因此我可以使用foreach ($line in $hereString),而当这种方法不起作用时,我尝试了Get-Content $hereString。无论哪种情况都没有喜悦。
我们的目标只是编写一些最终将操纵整个文本文件的代码,因此我可以在实现此功能的同时快速转换为使用一个小的示例文本文件,但是现在我很好奇HereStrings是否可以逐行使用,而不进行任何拆分。

最佳答案

由于您的意图是使用它来处理文件,因此我将从以下内容开始:

$file = 'c:\testfiles\testfile1.txt'

$textdata = 
@'
Line1
Line2
Line3
'@

$textdata | Set-Content $file

$lines = Get-Content $file

foreach ($line in $lines) {}

现在,您正在与生产环境相同的环境中进行测试,并且可以尝试使用不同的I / O方法以及不同的数据解析和操作方法。从性能的 Angular 来看,知道如何执行此操作将变得越来越重要,因为所涉及的文件的大小和数量会逐渐增加。

关于powershell - 逐行处理HereString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778508/

相关文章:

powershell - 有没有办法在Powershell中以编程方式检查文件上的数字签名?

powershell - 如何在powershell中为herestring/heredoc设置编码?

asp.net - 如何查找 IIS 功能的名称

ssh - 运行包含带 screen 的 STDIN 重定向的命令

bash - 在 bash 中将 **heredoc** 添加到文件中

bash - 为什么 bash 中的 read 命令可以使用这里字符串,但不能使用管道输出?

powershell - 如何从powershell为cmd脚本保留彩色输出(stdout和stderr)?

c# - PowerShell,读/写 SSH.NET 流

Powershell 报告列出修补程序