1

I have an xml file and I need to read only a particular substring from the main string. The xml file looks like below:

<?xml version="1.0" encoding="utf-8"?>
<Report Version="10.0">
<Entities>
<Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">
<Mods>
<Mod Name="svcUtility.Host.dll" AssemblyVersion="1.0.2000.001">
<Fields>
<Field Name="TIndex" Value="100" />            
<Field Name="Vindex" Value="200" />
</Fields>
</Mod>
</Mods>
</Entity>
</Entities>
</Report>

The main string in this xml is -

<Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">

And I need to print only the "appname" from it.

What condition logic can I use to print this using regex in powershell? And it need not be \wcf after the appname always.. it can be anything based on the dll path. For Eg, it can be like:

<Entity Name="\\sharing\Data\*SB*\**appname**\**Web**\Utilitysvc\bin\svcUtility.Host.dll">

or

<Entity Name="\\sharing\Data\*SB*\*DEVCS*\**appname**\**junk**\Utilitysvc\bin\svcUtility.Host.dll">

Can I have a generic select -string way? need to test this as well..

Thanks,
Ashish

2 Answers 2

2

This is a way:

$xml = [xml](get-content .\my.xlm )

 ($xml.Report.Entities.Entity.name | 
% { [regex]::matches($_, 'SB\\(.*)\\wcf') } |
 select -expand groups)[1].value

without [regex] .net method:

($xml.Report.Entities.Entity.name |
select-string 'SB\\(.*)\\wcf' -AllMatches | select -ExpandProperty matches |
select -ExpandProperty groups)[1].value

Edit:

try this pattern based on your last comment:

 ($xml.Report.Entities.Entity.name |
    select-string '(?<=\\Data\\.*\\)[^\\]*' -AllMatches |
    select -ExpandProperty matches |
    select -ExpandProperty groups)[0].value
Sign up to request clarification or add additional context in comments.

8 Comments

your method using the regex works.. but the \wcf can change based on the dll path. Can I have a generic select -string way? Also, before the appname it can be either SB or AB.. How can I test this with an OR?
@Christian.. Sorry it could be either SB or DEVCS before the appname.. and I tried with \(.*)\\.*' but it tags along everything after the appname..
I have around 300 odd xml files wherein the path can change depending on the dll.. so, apart from wcf or web it can be anything from A-Z :(
@ashishg. Re-edited my answer with regex lookbehind assertion... this works hoping the path have always DATA inside..
it works partially.. how can i chk to ommit anything that appears after appname.. it can be anything from A-Z, not necessarily only wcf or web.
|
1

You can do that without the complexity of a regex, split the path and grab the 5th element (sounds like a movie name):

[xml]$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<Report Version="10.0">
<Entities>
<Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">
<Mods>
<Mod Name="svcUtility.Host.dll" AssemblyVersion="1.0.2000.001">
<Fields>
<Field Name="TIndex" Value="100" />            
<Field Name="Vindex" Value="200" />
</Fields>
</Mod>
</Mods>
</Entity>
</Entities>
</Report>
"@

$xml.Report.Entities.Entity.Name.split('\')[5]

**appname**

1 Comment

It's not necessary that the appname is always at the same position.Please see Edit in my question. How can I use your code with an if condition. For Eg: if (Entity Name="\\sharing\Data\SB**appname**\wcf\Utilitysvc...) then $xml.Report.Entities.Entity.Name.split('\')[5] else $xml.Report.Entities.Entity.Name.split('\')[6]

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.