0

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).

2 Answers 2

4

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thank you! it also works! but I prefer the XML solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.