Reputation: 41575
I have the following file:
<Mapping ServerPath="$/Test/Dev/test/sunday/project" LocalPath="$(SourceDir)\Test" Version="Latest" WorkspaceMappingType="Map">
<Mapping ServerPath="$/Test/Dev/test/aaa/project2" LocalPath="$(SourceDir)\Test\project" Version="Latest" WorkspaceMappingType="Map">
<Mapping ServerPath="$/Test/Dev/test/sunday/project/test" LocalPath="$(SourceDir)\Test-machine" Version="Latest" WorkspaceMappingType="Map">
<Mapping ServerPath="$/Test/Dev/test/besaide/monday" LocalPath="$(SourceDir)\Monday" Version="Latest" WorkspaceMappingType="Map">
I need to extract from each line these 2 values:
ServerPath="$/Path"
LocalPath="$(SourceDir)\Path"
I have the file content in PS variable with this method:
$file = Get-Content C:\test\file.txt
Now how can I retrieve the 2 valus from each line with Regex? (or without Regex if it possible).
Upvotes: 0
Views: 649
Reputation: 156
To extract those two paths:
$input_path = 'C:\inputpath.txt'
$output_file = 'C:\outputpath.txt'
$regex = 'ServerPath=[^\s]+|LocalPath=[^\s]+'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file
Upvotes: 1
Reputation: 174485
Apart from the lack of closing tags, the input you've shown looks like an XML variant. If that's the case, I'd suggest using XML tools instead of regex:
Select-Xml -Path path\to\file.xml -XPath '//Mapping[@ServerPath and @LocalPath]'|%{
# grab the values of these
$_.Node.ServerPath
$_.Node.LocalPath
}
Upvotes: 4