J.W.
J.W.

Reputation: 11

Split Text File based on Strings

I want to split a text file into several files by determining the beginning and end of each file by a specific string. The beginning of the first file is identifiable by the line "<ca>", the end by "</ca>". Now I want to cut and paste the content in between those two strings in a new text file. Until now I've written this code:

$content = Get-Content .\*.txt
{
    if ($f -eq "</ca>") { $c > .\file.txt; }
    if ($f -ne "<ca>" -and $f -ne "</ca>") { $c += $f }
}

The second "if" is supposed to delete the "identification strings" from the created file.

I ran into two issues:

The file is a VPN-Configuration and looks like this:

client
dev tun
proto udp
remote 448
verify-x509-name
<ca>
Certificate:
Data:
    Version: 3 (0x2)
    Signature Algorithm: md5WithRSAEncryption
    Issuer: C=de
    -----BEGIN CERTIFICATE-----
MIICzDCCAjWgAwIBAgIJANfh65DfDF45GFSD
    -----END CERTIFICATE-----
</ca>
<cert>  
Certificate:
    Data:
        Version: 3 (0x2)
        Signature Algorithm: sha1WithRSAEncryption
        Issuer: C=de
</cert>
<key>
-----BEGIN RSA PRIVATE KEY-----
AoGBAN/jBWwRnjNtxJ+bj3U5oKhYjfu33N2dGlM9x5un9YLm9k6pBzhvG
</key>

The output looks like that:

clientdev tunproto udpremote 448verify-x509-name<ca>Certificate:...

(and so on)

Upvotes: 1

Views: 6688

Answers (3)

Repentance Tolife
Repentance Tolife

Reputation: 1

Oneliner to split file by search string on Powershell.

Split files by content string, run in Powershell:

$i=0;(gc Testfile.txt -raw) -split '#'|%{$i++;sc Testfile$i.txt -value $_}

This example splits Testfile.txt to multiple files with incremental number, Testfile1.txt, Testfile2.txt, etc...whenever the string '#' is found in the original file. The '#'-is also removed in the process.

Quite useful, I found it on the internet and decided to share it.

Upvotes: 0

colsw
colsw

Reputation: 3336

You're better off doing this with a multiline regex.

Get-Content .\vpnconfig.txt -Raw | Select-String '(?sm)<ca>(.+)</ca>' | Select -Expand Matches | Select -First 1 -Expand Value

Makes sure you use -Raw when using Regex like this.

Upvotes: 3

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200493

Read the file as a single string and use a regular expression match for extracting the CA certificate:

$config = Get-Content 'C:\path\to\your.ovpn' -Raw

if ($config -match '(?ms)<ca>(.*?)</ca>') {
    $matches[1].Trim() | Set-Content 'ca.crt'
} else {
    'No CA certificate found.'
}

Prior to PowerShell v3 use Get-Content | Out-String to get the content of the file as a single string.

Upvotes: 0

Related Questions