我与以下内容的源的文件:移调行列的PowerShell
0 ABC 1 181.12 2 05/07/16 4 Im4thData 5 hello -1 0 XYZ 1 1333.21 2 02/02/16 3 Im3rdData 5 world -1 ...
的“-1”在以上列表是记录分隔符,其指示下一个记录的开始。 0,1,2,3,4,5等就像列标识符(或列名称)。
这是我的代码如下。
$txt = Get-Content 'C:myfile.txt' | Out-String
$txt -split '(?m)^-1\r?\n' | ForEach-Object {
$arr = $_ -split '\r?\n'
$indexes = 1..$($arr.Count - 1) | Where-Object { ($_ % 2) -ne 0 }
$arr[$indexes] -join '|'
}
上面的代码创建输出象下面这样:
ABC|181.12|05/07/16|Im4thData|hello XYZ|1333.21|02/02/16|Im3rdData|World ...
但我需要输出类似下面。如果源文件中没有列,那么它们的行数据在输出文件中应该有下面的空行管道(||
)。请告知代码中需要的更改。
ABC|181.12|05/07/16||Im4thData|hello ← There is no 3rd column in the source file. so blank pipe line (||). XYZ|1333.21|02/02/16|Im3rdData||World ← There is no 4th column column in the source file. so blank pipe line (||). ...
你总是有数据只有一行?即每隔第二行总是内容,还是可以保存多行值? – arco444
@ arco444是始终有一行数据,没有多行值。 – MaheshMohan