I need to capture the data for every line that has an "*" in position 7 between IDENTIFICATION DIVISION and ENVIRONMENT DIVISION.
All other data can be ignored and once I hit Environment division I can exit the file.
IDENTIFICATION DIVISION and ENVIRONMENT DIVISION both start in position 8.
Here is an example of my DATA
IDENTIFICATION DIVISION.
**************************************************************** 00000700
**************************************************************** 00000800
* PURPOSE: TO PERFORM THE I/O LOGIC TO RETRIEVE THE CREDITING * 00000900
* AGENTS FOR THE SPLIT AGREEMENTS FOR A CUSTOMER * 00001000
* OR TERRITORY. * 00001100
* CUSTOMIZED VERSION OF ADMLR301 FOR MONTHLY DST * 00001110
* PROCESS-AAGYZ105. * 00001110
**************************************************************** 00001300
ENVIRONMENT DIVISION.
Here is code from @Olaf below. I added the logic to create add and close an excel spreadsheet. Works well!
Thank you @Olaf!!!
$excel = New-Object -ComObject excel.application
$excel.visible = $False
$workbook = $excel.Workbooks.Add()
$diskSpacewksht= $workbook.Worksheets.Item(1)
$diskSpacewksht.Name = "CAPTURE"
$col1=3
$diskSpacewksht.Cells.Item(2,8) = 'Header - Deletes'
$diskSpacewksht.Cells.Item(3,1) = 'Program'
$diskSpacewksht.Cells.Item(3,2) = 'Description'
$CBLFileList = Get-ChildItem -Path 'C:TEMP' -Filter '*.cbl' -File -Recurse
$Flowerbox = @()
ForEach($CBLFile in $CBLFileList) {
Write-Host "Processing ... $CBLFile" -foregroundcolor green
Get-content -Path $CBLFile.FullName |
ForEach-Object {
if ($_ -match 'IDENTIFICATION DIVISION') {
$treat = $true
}
if ($_ -match 'ENVIRONMENT DIVISION') {
$col1++
$diskSpacewksht.Cells.Item($col1,1) = $CBLFile
$diskSpacewksht.Cells.Item($col1,2) = [String]$Flowerbox
$Flowerbox = @()
continue
}
if ($treat) {
if ($_ -match '*(.{60})') {
Foreach-Object {$Flowerbox += $matches[1] + "`r`n"}
}
}
}
}
$excel.DisplayAlerts = 'False'
$ext=".xlsx"
$path="C:MISCCAPTURE$ext"
$workbook.SaveAs($path)
$workbook.Close
$excel.DisplayAlerts = 'False'
$excel.Quit()
question from:
https://stackoverflow.com/questions/65889041/powershell-capturing-all-lines-between-two-values-and-saving-the-captured-line 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…