Solved

Powershell - Parse strings to build array

Posted on 2010-09-06
6
1,327 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

751 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