Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

Powershell - Capturing all lines between two values and saving the Captured lines to a file that Excel can read

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If your input file always has the format you showed in the snippet your task might be easier than you think: ;-)

$CBLFileList = 
Get-ChildItem -Path 'D:sample' -Filter '*.cbl' -File -Recurse
$output = 
ForEach($CBLFile in $CBLFileList) {
    Get-content -Path $CBLFile.FullName |
    ForEach-Object {
        if ($_ -match 'IDENTIFICATION DIVISION') {
            $treat = $true
        }
        if ($_ -match 'ENVIRONMENT DIVISION.') {
            continue
        }
        if ($treat) {
            if ($_ -match '*s(.{60})') {
                [PSCustomObject]@{
                    Path = $CBLFile.FullName
                    Catch = $Matches[1]
                }
            }
        }
    }
}
$output

The code ignores all lines of the input file until it hits the first line with 'IDENTIFICATION DIVISION' then parses all lines until it hits the first line with 'ENVIRONMENT DIVISION'.

That should output what you're after. I just don't think it would be a good idea to pipe it to a CSV file.

The regex is looking for some arbitrary charachters following a single asterisk followed by one or more white spaces and followed by one or more white spaces followed by a single asterisk.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...