我有一個包含 1GB 資料的檔案。這些資料實際上是數千個單獨的迷你檔案中的 10 個。我需要提取每個單獨的檔案并將它們放在自己單獨的 Distinct 檔案中。所以本質上,我需要從單個檔案到 30K 單獨的檔案。
這是我的檔案的示例。
FILENAM1 VER 1 32 D
10/15/87 09/29/87
由 ?????
修訂者?????
說明 用戶域
記錄檔案名稱 1 版本 D 后綴 -4541
100 05 ST-CTY-CDE-FMHA-4541 顯示
200 10 ST-CDE-FMHA-4541 9(2) 顯示
300 10 CTY-CDE-FMHA-4541 9(3)顯示
400 05 NME-CTY-4541 X(20) 顯示
500 05 LST-UPDTE-DTE-4541 9(06) 顯示
600 05 填料 X 顯示 1 報告編號 08
資料字典報告器 REL 17.0 09/23/21
第 2 頁 DREPORT 008
記錄報告-****************************************************** ****************************************************** ************************************ 記錄記錄----日期----
記錄名稱長度生成器型別出現次數更新已創建
************************************************ ****************************************************** ************************************ 0
FILENAM2 VER 1 176 D
03/09/98 02/21/84
編制 ??????
修訂者??????
定義
I Need split the files out based upon a match of VER in position 68, 69 and 70. I also need to name each file uniquely. That information is stored on the same line in position 2-9. In the example above that string is "FILENAM1" and FILENAM2".
So just using the example above I would create two output files and they would be named FILENAM1.txt and FILENAM2.txt.
Since I have 30K files I need to split, doing this manually is impossible.
I do have a script that will split a file into multiple files but it will not search for strings by position.
Would anyone be able to assist me with this?
Here is script that DOES NOT Work. Hopefully I can butcher it and get some valid results....
$InputFile = "C:\COPIES.txt"
$Reader = New-Object System.IO.StreamReader($InputFile)
$OPName = @()
While (($Line = $Reader.ReadLine()) -ne $null) {
If ($Line -match "VER"(67,3)) {
$OPName = $Line.(2,8)
$FileName = $OPName[1].Trim()
Write-Host "Found ... $FileName" -foregroundcolor green
$OutputFile = "$FileName.txt"
}
Add-Content $OutputFile $Line
}
Thank you in advance,
-Ron
uj5u.com熱心網友回復:
我建議使用一個switch陳述句,它提供方便和快速的逐行讀取檔案 via-File和regex -matching via -Regex:
$streamWriter = $null
switch -CaseSensitive -Regex -File "C:\COPIES.txt" {
'^.(.{8}).{58}VER' { # Start of a new embedded file.
if ($streamWriter) { $streamWriter.Close() } # Close previous output file.
# Create a new output file.
$fileName = $Matches[1].Trim() '.txt'
$streamWriter = [System.IO.StreamWriter] (Join-Path $PWD.ProviderPath $fileName)
$streamWriter.WriteLine($_)
}
default { # Write subsequent lines to the same file.
if ($streamWriter) { $streamWriter.WriteLine($_) }
}
}
$streamWriter.Close()
注意:使用型別.Substring()方法的解決方案[string]也是可能的,但會更冗長。
正則運算式的
^.(.{8}).{58}部分匹配每行的前 67 個字符,同時通過捕獲組捕獲(基于 1 的)第 2 到 9 列(檔案名)(.{8})中的那些,這使得捕獲的文本在自動變數[1]的索引中可用。然后,正則運算式的一部分確保該行僅在第 68 列位置找到時才匹配。$MatchesVERVER為了高效地創建輸出檔案,
[System.IO.StreamWriter]使用了實體,這比逐行Add-Content呼叫要快得多。此外,Add-Content您必須確保目標檔案不存在,因為現有內容將被附加到。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/433702.html
