Reputation: 11
I need to extract a list with strings that are between two special characters (= and ;). Below is an example of the file with line types and the needed strings in bold. File is a quite big one, type is xml.
<type="string">data source=**HOL4624**;integrated sec>
<type="string">data source=**HOL4625**;integrated sec>
I managed to find the lines matching “data source=”, but how to get the name after? Used code is below.
Get-content regsrvr.txt | select-string -pattern "data source="
Thank you very much!
<RegisteredServers:ConnectionStringWithEncryptedPassword type="string">data source=HOL4624;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096</RegisteredServers:ConnectionStringWithEncryptedPassword>
<RegisteredServers:ConnectionStringWithEncryptedPassword type="string">data source=HOL4625;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096</RegisteredServers:ConnectionStringWithEncryptedPassword>
Upvotes: 1
Views: 1328
Reputation: 3036
You can expand your try at using Select-String
with a better use of regex. Also, you don't need to use Get-Content
first. Instead you can use the -Path
parameter of Select-String
.
The following Code will read the given file and return the value between the =
and ;
:
(Select-String -Path "regsrvr.txt" -pattern "(?:data source=)(.*?)(?:;)").Matches | % {$_.groups[1].Value}
Pattern Explanation (RegEx):
You can use -pattern
to capture an String given a matching RegEx. The Regex can be describe as such:
(?:
opens an non-capturing Group
data source=
matches the charactes data source=
)
closes the non-capturing Group
(.*?)
matches any amount of characters and saves them in a Group. The ?
is the lazy operator. This will stop the matching part at the first occurence of the following group (in this case the ;
).
(?:;)
is the final non-capturing Group for the closing ;
Structuring the Output
Select-String
returns a Microsoft.PowerShell.Commands.MatchInfo
-Object.
You can find the matched Strings (the whole String and all captured groups) in there. We can also loop through this Output and return the Value of the captured Groups: | % {$_.groups[1].Value}
%
is just an Alias for For-Each
.
For more Informations look at the Select-String
-Documentation and try your luck with some RegEx.
Upvotes: 1
Reputation: 24071
Since the connectionstring is for SQL Server, let's use .Net's SqlConnectionStringBuilder to do all the work for us. Like so,
# Test data, XML extraction is left as an exercise
$str = 'data source=HOL4624;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096'
$builder = new-object System.Data.SqlClient.SqlConnectionStringBuilder($str)
# Check some parameters
$builder.DataSource
HOL4624
$builder.IntegratedSecurity
True
Upvotes: 1
Reputation: 8889
The XML is not valid, so it's not a clean parse, anyway you can use string split with regex match:
$html = @"
<RegisteredServers:ConnectionStringWithEncryptedPassword type="string">data source=HOL4624;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096</RegisteredServers:ConnectionStringWithEncryptedPassword>
<RegisteredServers:ConnectionStringWithEncryptedPassword type="string">data source=HOL4625;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096</RegisteredServers:ConnectionStringWithEncryptedPassword>
"@
$html -split '\n' | % {$null = $_ -match 'data source=.*?;';$Matches[0]} |
% {($_ -split '=')[1] -replace ';'}
HOL4624
HOL4625
Upvotes: 1