0

I'm returning some data like this in powershell :

1)Open;#1

2)Open;#1;#Close;#2;#pending;#6

3)Closed;#5

But I want an output like this :

1)1 Open

2)

1 Open

2 Close

6 pending

3)

5 Closed

The code:

$lookupitem = $lookupList.Items
$CMRSItems = $list.Items | where {$_['ID'] -le 5}
$CMRSItems | ForEach-Object {

$realval =  $_['EventType']
Write-Host "RefNumber: " $_['RefID']
Write-Host $realval

}

Any help would be appreciated as my powershell isn't that good.

1
  • Can you show the code that gives this output. It's perhaps easier to modify this code than write more code to change the output. Commented Mar 12, 2014 at 5:37

1 Answer 1

1

Without regular expressions, you could do something like the following:

Ignore everything up to the first ')' character
Split the string on the ';' character
foreach pair of the split string
    the state is the first part (ignore potentially leading '#')
    the number is the second part (ignore leading '#')

Or you could do it using the .NET System.Text.RegularExpressions.Regex class with the following regular expression:

(?:#?(?<state>[a-zA-Z]+);#(?<number>\d);?)

The Captures property on the MatchCollection returned by the Matches method would be a collection in which each item will contain two instances in the Group collection; named state and number respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the tip Robert.But not that good with powershell :( Also the string i want to manipulate is "Open;#1;#Close;#2;#pending;#6".this is also a dynamic string not hardcoded
The format of the strings, e.g State1;#Num1;#State2;#Num2 is fixed (if it wasn't you'd have a hard time parsing it without a learning algorithm) and both of the above suggested solutions would work well for dynamic strings. I would probably suggest using the regular expression solution. Read up on using .NET classes from PowerShell and read up on the System.Text.RegularExpressions.Regex documentation. If you get stuck after having tried, please make a new question asking about help where you're stuck (and add a link here to the new question as a comment to this answer if you do get stuck).

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.