[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell String Parsing

Posted on 2016-11-15
3
Medium Priority
?
99 Views
Last Modified: 2016-11-15
I am reading in data from a 'configuration' file, made up of a key and a value. One of the values I want to read in is a string formatted as such:
server1,database1,table1|server2,database2,table2|server3,databse3,table3[|...]
I want to then parse this into a table/array that will contain:
SERVER   DATABASE   TABLE
server1   database1    table1
server2   database2    table2
server3   database3    table3
...

That I can use in a foreach loop such as
ForEach (rowData in dataRows)
{
    $server = rowData.element1
    $database = rowData.element2
    $table = rowData.element3
    ...
}

Note that the elements of each row will be comma delimited and each row will be pipe delimited. Note sure I can get titles but they aren't necessary if I can just reference the element in the rowData.
0
Comment
Question by:dbbishop
[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
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41888348
$dataRows = 'server1,database1,table1|server2,database2,table2|server3,databse3,table3' -split '\|' | ConvertFrom-Csv -Header Server, Database, Table

Open in new window

Or when reading from a file:
$dataRows = (Get-Content .\file.txt) -split '\|' | ConvertFrom-Csv -Header Server, Database, Table

Open in new window

And then
ForEach ($row in $dataRows)
	$row.Server
	# ...

Open in new window

0
 
LVL 85

Expert Comment

by:oBdA
ID: 41888376
Sorry, forget about the part with reading from a file, missed the part with the "key and a value". Just in case: you don't need to reinvent the wheel to read a file like that, ConvertFrom-StringData will do it.
If it's really just "key = value" pairs in the file, like this:
FirstKey = server1,database1,table1|server2,database2,table2|server3,databse3,table3
SecondKey = foo
ThirdKey = bar

Open in new window

Then you can read it like that:
$Config = Get-Content C:\Temp\config.txt | ConvertFrom-StringData
$dataRows = $Config.'FirstKey' -split '\|' | ConvertFrom-Csv -Header Server, Database, Table
ForEach ($row in $dataRows)
	$row.Server
	# ...
}

Open in new window

1
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 41888425
Works like a champ and super quick response. Thanks.
0

Featured Post

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

656 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