Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell - Parse strings to build array

Posted on 2010-09-06
6
Medium Priority
?
1,331 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:chad
  • 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 2000 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
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
LVL 11

Author Comment

by:chad
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 2000 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:chad
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
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…
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 anti-spam), the admin…

916 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