Solved

Powershell - Parse strings to build array

Posted on 2010-09-06
6
1,319 Views
Last Modified: 2012-05-10
I have a string that contains about 200 characters and I would like to parse it for certain items.

[array]$a = "alsdkf laskdjlfkajsldjf asldkjf laksjdflk cat@dog1' alkjflkasdf asdfasdfasdf"
$a = $a + "asldjaflsdkjfalskjdflkajkcat@dog2'alsdkjf;alsdkjafoeidjfke

essentially I would like to have a result of  from the above array.
dog1
dog2

the above string is read from a txt file and may contain single and double quotes.
the values I need will always follow a cat@ and will always be followed by a single quote.
I have tried working with trimstart and replace but I am missing an understanding of some of their features.


Thanks,
K
0
Comment
Question by:kabaam
  • 4
  • 2
6 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 33613417
What about this:
$a | Select-String "cat@(\w+)'" -AllMatches | %{$_.matches[0].groups[1].value}

Open in new window

0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33613422
Actually, if there is only 1 occurance of the ca@dog structure in each line, it is enough:
$a | Select-String "cat@(\w+)'" | %{$_.matches[0].groups[1].value}

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 33613450
If multiple dogs can be there in each line:
$a | Select-String "cat@(\w+)'" -AllMatches | %{$_.matches} | %{$_.groups[1].value}

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 11

Author Comment

by:kabaam
ID: 33613666
Thanks Soostibi,

I will double the points if you could walk me through the code so I can better understand what I now have.

K
0
 
LVL 13

Assisted Solution

by:soostibi
soostibi earned 500 total points
ID: 33613699
Select-string is a complex cmdlet, it selects the matching lines of strings and also gives the result of the matches.
By default it searches for the first match in each string it gets from the pipeline, but with the -AllMatches it gives all matches. What it gives as a result, is an array of complex objects, its matches property includes all the matches, and the groups property of the matches property gives all the group results. Group result means the whole result (in our case cat@....') and all the sub-matches (in our case the part that is in brackets between the @ and the '). This is what we want, that is the second (index 1) in the groups property, and its value property is the group match.
I suggest for you to put a "format-list *" after every pipe character (|) to see what's in the pipeline in every section, so try:
$a | Select-String "cat@(\w+)'" -AllMatches | format-list *
$a | Select-String "cat@(\w+)'" -AllMatches | %{$_.matches} | format-list *
0
 
LVL 11

Author Comment

by:kabaam
ID: 33613739
Thanks again.  

I really do not understand the piping yet. And anytime I see $_. I am lost.  I need to work on understanding that.

Thanks,
K
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now