Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell - Parse strings to build array

Posted on 2010-09-06
6
Medium Priority
?
1,330 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 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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 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: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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A brief introduction to what I consider to be the best editor for PowerShell.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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