How can I convert a delimited string to a hash table / array using Powershell? - powershell

I am looking to convert a collection of comma delimited strings that I have as a variable into a hash table / array in Powershell.
For example
$test =
Heading1,Heading2,Heading3
Line1,Line1,Line1
Line2,Line2,Line2
Is there a way I can easily convert this to a hash table or array so that I can easily retrieve all data under Heading3?

Variable was already in csv format so just had to use convertto-csv $test to get it into hash table / array format.
Thank you to Olaf in the comments for this answer!

Related

Splitting on column into multiple coloums from a CSV file in powershell

I am new to using powershell and I am in need of some assistance.
I have a csv file that looks like this:
DisplayName,AllJSSUSers,ALLMobileDevices,LimitToUsers,Exclusions,DepartmentEx,IconURL,ID
Aurasma,TRUE,TRUE,"G_Year 4,G_Year 7,G_Year 11,G_Year 6,G_Year 10,G_Year 5,G_Year 9,G_Teaching Staff,G_Year 8,G_Supply Teachers,G_Year 3,G_Year 12",,,,5
What I would like to do is split the column "LimitToUsers" where the commas are into multiple column and then output that to a new csv file.
I have no idea where to start with this. Can anyone help?
Thank you
Gavin
You can read CSV data with Import-Csv.
You can access that column from each data object by accessing the LimitToUsers property.
You can split a string with the -split operator.
You can add new properties to object with Add-Member.
You can write CSV with Export-Csv.
Since you somehow have to split a single column into multiple ones, how you do that is up to you and I can't help you there

Writing Tables from Matlab into CSV

I have several tables in matlab that and I would like to write all to one .csv file, vertically concatenating. I would like to keep the column names from each table as the top row, and would like to use a loop to write the csv. The ultimate goal is to read the data in to R, but R.matlab did not work well. Suggestions about how to do this?
Alternatively how can I change filenames in a for loop using the iterator?
e.g. along the lines of
for i=1:10
writecsv('mydatai.csv',data(i))
end
So I must have at the end 10 csv files as output.
You can change the filename within the loop by using for sprintf string formatting function, for example:
dlmwrite(sprintf('mydata%i.csv', i), data(i) )
Note that the %i portion of the string is the sprintf formatting operator for an integer, it is just a coincidence that you also decided to name your iterator variable 'i'.
You can append extra data to an existing CSV by using the dlmwrite function, which uses a comma delimiter as the default, and including the '-append' flag.
Another way would be to use
writetable(Table,filename )
and to change file name after every alternation you can use
filename = ['mydata' num2str(i) '.csv']

Array to CSV, not the right format

I used the ">" operator to put the content of an array into a CSV file, here is what i got, the first line is empty
Computer IP
-------- --
IMPPRD1 172.22.30.33
IMPPRD2 172.22.30.31
IMPPRD3 172.22.30.32
IMPSR1 172.22.30.12
IMPPRD5 172.22.30.17
I would like it to be a normal CSV, so something like this :
Computer,IP
IMPPRD1,172.22.30.33
IMPPRD2,172.22.30.31
IMPPRD3,172.22.30.32
IMPSR1,172.22.30.12
How could I manage to do this using powershell?
Thanks !
Using > outputs as normal text - including the spaces between columns. Basically the same as Write-Output. You are looking for Export-Csv which outputs in CSV format.

How to convert string to hashtable in 1 go?

This is strictly a learning experience:
I have a .CSV file that I'm using to define my deployment environments. One of the variables has to be in a Hash Table format.
Can anyone come up with a clever way to put it all in one line?
Right now I harvest them as a string from CSV, conver to array, convert array to hash table.
Simplified code:
Foreach($i in $DefaultCSV){...
$App_Fabric_Hosts_a = $i.App_Fabric_Hosts.split(",")}
$App_Fabric_Hosts_h = #{}
foreach($r in $App_Fabric_Hosts_a){$App_Fabric_Hosts_h.add($r,"22233")}
This is the best I came up with:
$d=#{};foreach($r in $DefaultCSV[$arrayposition].app_fabric_hosts.split(",")){$d.add($r,"22233")}

How can I convert an Excel file to CSV with Perl's Spreadsheet::WriteExcel?

I have an Excel sheet and am able to convert it into .csv format using Perl. My only problem is that some of my data in the Excel sheet cells contain commas and that
has to be retained in the CSV format also, but while converting it takes as a
seperator. How can I retreive the data in the cell with commas as it is and print it in
the CSV?
For example, in the Excel sheet A1 cell contains the data {0xAAAA,0xFFFF,0xAAAA,0xAAAA} I want the same data with commas in the A1 cell in CSV file also. I am Spreadsheet::ParseExcel.
Use Text::CSV for parsing and creating your csv files.
Be more specific if you do not know how to use the module for your task.
To avoid the commas in your fields to be interpreted as separators, enclose your fields with characters like double quotes:
"10","10,00","156"