【发布时间】:2020-03-11 17:20:36
【问题描述】:
【问题讨论】:
-
你说的第一个空格是什么?请编辑您的问题并将您拥有的文本添加为格式化文本,而不是图像链接。同时提供所需输出的样本。
-
一行中第一个连续的空格,可以使用
-replace '(?<=^\S+)\s+',"`t"
标签: regex powershell split
【问题讨论】:
-replace '(?<=^\S+)\s+',"`t"
标签: regex powershell split
这应该可以解决问题:
$sourceFilePath = 'c:\infile.txt'
$destFilePath = 'c:\outfile.txt'
$writeHandle = [System.IO.File]::OpenWrite( $destFilePath )
foreach($line in [System.IO.File]::ReadLines($sourceFilePath))
{
$outbuf = [byte[]][char[]](($line -replace '^(.*?) (.*?) (.*?) (.*?) (.*)$', '$1*$2*$3*$4*$5').Replace("*", "`t") + [environment]::NewLine)
[void]$writeHandle.Write( $outbuf, 0, $outbuf.Length )
}
[void]$writeHandle.Close()
【讨论】:
你可以使用这样的东西
$inputtext = Get-Content 'EQ-Input.txt'
$outputobject = foreach ($Line in $inputtext) {
$arr = $line -split ' '
[pscustomobject]@{
Date = $arr[0]
Time = $arr[1]
Code = $arr[2]
Result = $arr[3..($arr.Length-1)] -join ' '
}
}
然后您可以使用$outputobject 进行进一步分析,也可以将其转换或保存为 CSV。
$outputobject | ConvertTo-Csv
$outputobject | Export-Csv -Path 'EQ-Output.csv'
【讨论】:
-Split ' ',4,它将简单地将数组的其余部分放入$arr[3]。