用制表符替换空格

我有一些文本内容,想在更友好的视图中进行拆分,然后导出为CSV格式。我想用tab代替前几个空格。我尝试使用正则表达式模式\s进行操作,但它会分割所有文本。

您可能会看到示例数据和我的结果

用制表符替换空格

lisports 回答:用制表符替换空格

您可以使用类似的东西

$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'
,

这应该可以解决问题:

$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,$outbuf.Length )
}

[void]$writeHandle.Close()
本文链接:https://www.f2er.com/3094770.html

大家都在问