Solved

Powershell - Parse strings to build array

Posted on 2010-09-06
6
1,328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

617 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