0

Below is the auto-generated example.txt file:

    Application Name:       XYZSNWJ
    Secret Key:             NDRkNDJcvWDFW
    Application Name:       XYZSNWKkms
    Secret Key:             DFSmnbaDFAsdc
    Application Name:       HBDFJHXPI
    Application Name:       XHjacvJHasxb 

And the output should be like below using regex or any other command:

    Application Name:       XYZSNWJ
    Secret Key:             NDRkNDJcvWDFW
    Application Name:       XYZSNWKkms
    Secret Key:             DFSmnbaDFAsdc

How to do it in powerShell.
The file is auto-generated, so there may be from 0 to n time blocks available in a file, like this:

    Application Name:       XYZSNWJ
    Secret Key:             NDRkNDJcvWDFW
    Application Name:       XYZSNWKkms
    Secret Key:             DFSmnbaDFAsdc
    Application Name:       HBDFJHXPI
    Application Name:       XHjacvJHasxb 
    Application Name:       XYZSNWJ
    Secret Key:             NDRkNDJcvWDFW
    Application Name:       XYZSNWKkms
    Secret Key:             DFSmnbaDFAsdc
    Application Name:       HBDFJHXPI
    Application Name:       XHjacvJHasxb 
    Application Name:       XYZSNWJ
    Secret Key:             NDRkNDJcvWDFW
    Application Name:       XYZSNWKkms
    Secret Key:             DFSmnbaDFAsdc
    Application Name:       HBDFJHXPI
    Application Name:       XHjacvJHasxb 
    Application Name:       XYZSNWJ
    Secret Key:             NDRkNDJcvWDFW
    Application Name:       XYZSNWKkms
    Secret Key:             DFSmnbaDFAsdc
    Application Name:       HBDFJHXPI
    Application Name:       XHjacvJHasxb 
    Application Name:       XYZSNWJ
    Secret Key:             NDRkNDJcvWDFW
    Application Name:       XYZSNWKkms
    Secret Key:             DFSmnbaDFAsdc
    Application Name:       HBDFJHXPI
    Application Name:       XHjacvJHasxb 

then how it should be repeated till the end of file.

Andrew
  • 124
  • 1
  • 12

1 Answers1

1

The following removes everything from the input starting with the first two consecutive lines that each start with Application Name:, possibly preceded by tabs and/or spaces, using the -replace operator.

@'
    Application Name:       XYZSNWJ
    Secret Key:             NDRkNDJcvWDFW
    Application Name:       XYZSNWKkms
    Secret Key:             DFSmnbaDFAsdc
    Application Name:       HBDFJHXPI
    Application Name:       XHjacvJHasxb 
'@ > example.txt

(Get-Content -Raw example.txt) -creplace
  '\r?\n[ \t]*Application Name:.+\r?\n[ \t]*Application Name:[\s\S]+$'
mklement0
  • 382,024
  • 64
  • 607
  • 775