我與以下內容的源的文件:移調行列的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