再會
我想計算檔案中一行(行)1中的所有“,”,然后將其與此后的每一行進行比較,如果第一行中的“,”較少,則在末尾添加差異“,”通過使用 Powershell,將該行轉換為一個新檔案。
如何創建缺少分隔符的新檔案?
任何想法和幫助將不勝感激。
資料檔案:
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7
1,2,3,4,5,6
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5
1,2,3,4
1,2,3
1,2
需要新的資料檔案:
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,,
1,2,3,4,5,6,7,,,
1,2,3,4,5,6,,,,
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,,,,,
1,2,3,4,,,,,,
1,2,3,,,,,,,
1,2,,,,,,,,
這是我的腳本:
$file = 'C:\Test\File1.csv'
$i = 1
$reader = [System.IO.File]::OpenText($file)
$line = $reader.ReadLine()
$reader.Close()
$delimiter = $line.Split(",").Count
$reader = [System.IO.File]::OpenText($file)
try {
for() {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
$c = $line.Split(",").Count
if($c -ne $delimiter -and $i -ne ${lines}) {
($d = $delimiter - $c)
if($c -ne $delimiter) {
$varline = $line ",".PadRight($d, ",")
$line -replace $line, $varline
Write-Host "Line number: $i"
Write-Host "Delimiters in line: $c"
Write-Host "Delimiters should be: $delimiter"
Write-Host "Diffrence: $d"
Write-Host "New Line: $varline"
}
}
$i
}
}
finally {
$reader.Close()
}
Set-Content "C:\Test\TestMod.csv" -Value $varline
寫主機輸出:(僅用于測驗目的,不需要)
1
1,2,3,4,5,6,7,8,9,
Line number: 2
Delimiters in line: 9
Delimiters should be: 10
Diffrence: 1
New Line: 1,2,3,4,5,6,7,8,9,
2
1,2,3,4,5,6,7,8,,
Line number: 3
Delimiters in line: 8
Delimiters should be: 10
Diffrence: 2
New Line: 1,2,3,4,5,6,7,8,,
3
1,2,3,4,5,6,7,,,
Line number: 4
Delimiters in line: 7
Delimiters should be: 10
Diffrence: 3
New Line: 1,2,3,4,5,6,7,,,
4
1,2,3,4,5,6,,,,
Line number: 5
Delimiters in line: 6
Delimiters should be: 10
Diffrence: 4
New Line: 1,2,3,4,5,6,,,,
5
1,2,3,4,5,,,,,
Line number: 7
Delimiters in line: 5
Delimiters should be: 10
Diffrence: 5
New Line: 1,2,3,4,5,,,,,
6
1,2,3,4,,,,,,
Line number: 8
Delimiters in line: 4
Delimiters should be: 10
Diffrence: 6
New Line: 1,2,3,4,,,,,,
8
1,2,,,,,,,,
Line number: 10
Delimiters in line: 2
Delimiters should be: 10
Diffrence: 8
New Line: 1,2,,,,,,,,
uj5u.com熱心網友回復:
您可以構建任意處理管道來修改文本檔案中的每一行并將結果輸出到新的文本檔案,如下所示:
Get-Content $outputPath |ForEach-Object { <# process file content here #> } |Set-Content $outputPath
由于除第一行之外的每一行的處理每次都包含相同的操作:
- 計算
,'s 的數量 - 計算從
,第一行的數量到計數的差異 - 附加尾隨
,的
...我們只需要兩個變數 - 一個用于跟蹤我們是否檢查了第一行,另一個用于保存逗號計數:
$inFile = 'C:\Test\File1.csv'
$outFile = 'C:\Test\output.csv'
$initialized = $false
$commaCount = 0
Get-Content -LiteralPath $inFile |ForEach-Object {
if(-not $initialized){
# first line, let's just count the commas
$commaCount = $_.Split(',').Length - 1
$initialized = $true
} else {
# subsequent lines, calculate the difference in number of commas
$commaCountDiff = $commaCount - ($_.Split(',').Length - 1)
if($commaCountDiff -gt 0){
# and add missing ones
$_ = "${_}$(',' * $commaCountDiff)"
}
}
# output the line (modified or not)
$_
} |Set-Content -LiteralPath $outFile
uj5u.com熱心網友回復:
您也可以通過回圈資料線兩次來做到這一點:
$data = Get-Content -Path 'C:\Test\File1.csv'
# first loop to get the maximum number of fields in the data
$maxFields = ($data | ForEach-Object {($_ -split ',').Count} | Measure-Object -Maximum).Maximum
# second loop to output the lines with extra commas (empty fields) appended
$data | ForEach-Object {
$_ ',' * ($maxFields - ($_ -split ',').Count)
} | Set-Content -Path 'C:\Test\TestMod.csv'
輸出:
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,,
1,2,3,4,5,6,7,,,
1,2,3,4,5,6,,,,
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,,,,,
1,2,3,4,,,,,,
1,2,3,,,,,,,
1,2,,,,,,,,
uj5u.com熱心網友回復:
非常感謝Mathias R. Jessen
這是對我有用的代碼:
$inFile = 'C:\Test\File1.csv'
$outFile = 'C:\Test\output.csv'
$initialized = $false
$commaCount = 0
Get-Content -LiteralPath $inFile |ForEach-Object {
if(-not $initialized){
# first line, let's just count the commas
$commaCount = $_.Split(',').Length - 1
$initialized = $true
} else {
# subsequent lines, calculate the difference in number of commas
$commaCountDiff = $commaCount - $_.Split(',').Length 1
if($commaCountDiff -gt 0){
# and add missing ones
$_ = "${_}$(',' * $commaCountDiff)"
}
}
# output the line (modified or not)
$_
} |Set-Content -LiteralPath $outFile
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/348775.html
