Home > Back-end >  Powershell - Remove lines that begin with multiple strings
Powershell - Remove lines that begin with multiple strings

Time:10-23

I am hoping someone can help me. I have extracted certificates from a PFX and would like to remove all lines that start with a space, "Bag Attributes", "issuer" and "subject". My input file would look something like this:

Bag Attributes
    friendlyName: BLAH BLAH
    BLAH BLAH BLAH
subject=C = BLAH BLAH BLAH 
issuer=C = BLAH BLAH BLAH 
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH 
-----END CERTIFICATE-----
Bag Attributes
    friendlyName: BLAH BLAH
    BLAH BLAH BLAH
subject=C = BLAH BLAH BLAH 
issuer=C = BLAH BLAH BLAH 
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH 
-----END CERTIFICATE-----
Bag Attributes
    friendlyName: BLAH BLAH
    BLAH BLAH BLAH
subject=C = BLAH BLAH BLAH 
issuer=C = BLAH BLAH BLAH 
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH 
BLAH BLAH BLAH 
-----END CERTIFICATE-----

And the output should look like the following:

-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH 
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH 
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH 
BLAH BLAH BLAH 
-----END CERTIFICATE-----

I have tried using the following which does remove lines beginning with spaces, however have not been able to successfully remove lines starting with "Bag Attrubutes","subject" and "issuer" with { $_ -notmatch "^ ","^subject","^issuer","^Bag Attributes" }

Get-Content "C:\ScriptRepository\Certs\CA-Chain.pem" | 
Where { $_ -notmatch "^ " } | 
Set-Content "C:\ScriptRepository\Certs\CA-chain2.pem"

Any help would be greatly appreciated.

CodePudding user response:

If you have that in a file, just use a switch with a very simple regex:

$result = switch -Regex -File 'X:\InputFile.pfx' {
    '^(Bag|subject|issuer|\s)'  { <# skip these lines #> }
    default { $_ }
}
$result | Set-Content -Path "C:\ScriptRepository\Certs\CA-chain2.pem"

Output:

-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH 
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH 
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH 
BLAH BLAH BLAH 
-----END CERTIFICATE-----

Regex details:

^                 Assert position at the beginning of the string
(                 Match the regular expression below and capture its match into backreference number 1
                  Match either the regular expression below (attempting the next alternative only if this one fails)
      Bag         Match the characters “Bag” literally
   |              Or match regular expression number 2 below (attempting the next alternative only if this one fails)
      subject     Match the characters “subject” literally
   |              Or match regular expression number 3 below (attempting the next alternative only if this one fails)
      issuer      Match the characters “issuer” literally
   |              Or match regular expression number 4 below (the entire group fails if this one fails to match)
      \s          Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
)
  • Related