I’m trying to solve a production problem. We receive an error file daily (Mon-Fri) from the bank that contains error records. These records are 94 bytes in length. On occasion there will be some error codes in the file that cause some significant problems when processed by a system at the State.
I was asked to “filter out” the error records from the file that gets sent to the State. To do this, I created a one line FINDSTR command (below) to locate records containing the error code “R02” (no quotes) in positions 4-6 of the records, and remove them.
      FindStr /V "R02" INPUT_FILE > OUTPUT_FILTERED_FILE_%DATE%_%TIME%
This worked as I had hoped and the requesting users were happy, BUT one of the managers found that the record immediately preceding the record containing the error code ALSO MUST BE DELETED from the file, because it is associated with the record containing the error code. The problem is that this preceding record does NOT contain an error code in it. There is a 6 digit number in positions 89-94 that could be related to the error record, but I don't want to guess, or over-complicate the script.
As you can see in the example data (below), there are 5 error records (5, 7, 9, 11, and 27), containing error code “R02.” My FindStr command worked for removing the R02 records and creating an output file without any of the error records (containing “R02”).
WHAT I NEED NOW is to be able to remove the “associated records” that go with the R02 records in the example data below. Done properly, the following records would be removed from the example file in a single process: 4, 5, 6, 7, 8, 9, 10, 11, 26 and 27.
I need to delete both the “R02 error records AND the associated record above each of those error records simultaneously, and write the output to a NEW file, leaving the original file intact AS-IS – because it is retained locally by our user department.
Below, is what the INPUT record content looks like (error codes in BOLD), with a record number pre-pended for reference purposes. Sorry, but I can’t supply the full 94 byte record images due to security issues. Below that, is what my desired output file should look like.
I don’t know if this can be done with FindStr, but I’m sure PowerShell can do the job, BUT I know nothing about PowerShell. The script will be executed on Windows Server.
Can anyone help me with creating a script that will accomplish the processing to transform the input file into the desired output file?
Thanks very much in advance for your assistance.
****** Example Data ******
Input File
Nbr - - Record Content - -
01    HEADER RECORD                    
02    CONTROL RECORD                   
03    5200SAN                          
04    62112200                         
05    799**R02**12                         
06    62112200                         
07    799**R02**12                         
08    62112200                         
09    799**R02**12                         
10    62112200                         
11    799**R02**12                         
12    82000000                         
13    5200SAN                          
14    62112200                         
15    798C0312                         
16    62112200                         
17    798C0312                         
18    62112200                         
19    798C0312                         
20    62112200                         
21    798C0312                         
22    62112200                         
23    798C0312                         
24    82000000                         
25    5200SAN                          
26    62112200                         
27    799**R02**12                         
28    TRAILER RECORD                   
Desired New Output File
Nbr - - Record Content - -
 01    HEADER RECORD                
 02    CONTROL RECORD               
 03    5200SAN                      
       #  DELETED  #                
       #  DELETED  #                
       #  DELETED  #                
       #  DELETED  #                
       #  DELETED  #                
       #  DELETED  #                
       #  DELETED  #                
       #  DELETED  #                
 12    82000000                     
 13    5200SAN                      
 14    62112200                     
 15    798C0312                     
 16    62112200                     
 17    798C0312                     
 18    62112200                     
 19    798C0312                     
 20    62112200                     
 21    798C0312                     
 22    62112200                     
 23    798C0312                     
 24    82000000                     
 25    5200SAN                      
       #  DELETED  #                
       #  DELETED  #                
 28    TRAILER RECORD