Solved

Powershell String Parsing

Posted on 2016-11-15
3
20 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
  • 2
3 Comments
 
LVL 83

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 83

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article will help you understand what HashTables are and how to use them in PowerShell.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now