Solved

Powershell String Parsing

Posted on 2016-11-15
3
72 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 500 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

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ā€¦
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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