Parsing ini file and adding it to a CSV file

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Parsing ini file and adding it to a CSV file

Post by sekou2331 »

I am trying to parse an ini file and then add it to a csv file with headers. I have asked this question before but went a different route with the parse. I multiple section like the below that i need to parse and put together in a csv file.


Header1 Header2 Header3
something1 something2 something3
something4 something5 something6

  • [Section_1]
    not need =1
    Primary_need1=something1
    Primary_need2=something2
    Primary_need3=something3
    Primary_Recovery_needed4=something4
    Primary_Recovery_needed5=something5
    Primary_Recovery_needed6=something6
    not need =2
    not need =3
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Parsing ini file and adding it to a CSV file

Post by jvierra »

There is no command or tool that will convert an INI file to a CSV. You will have to write a script that does whatever conversions you need. As posted you have not provided any useful information on how the file needs to be converted. Once you can define that you will know how to write the script.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Parsing ini file and adding it to a CSV file

Post by sekou2331 »

Ok so I hope this is a better question. In the ini file there are multiple section like the below. How can grab everything that say primary and keep it in the order that it is in for each section? So basically using this example below grab everything from Primary_need1=something1 to Primary_Recovery_needed6=something6 and leave out everything else?



[Section_1]
not need =1
Primary_need1=something1
Primary_need2=something2
Primary_need3=something3
Primary_Recovery_needed4=something4
Primary_Recovery_needed5=something5
Primary_Recovery_needed6=something6
not need =2
not need =3
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Parsing ini file and adding it to a CSV file

Post by sekou2331 »

Ok after working on it I was able to parse the ini file and get the information i needed by using the below code. I don't know if this is the best method. But it gives me the below the only thing I am having an issue with is taking 3 of the string and making it one string. i used a foreach and then join but it is not working.


IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object

Code: Select all

$ConfigFile = sls -path .\file.ini -Pattern 'Primary', 'Secondary'  -SimpleMatch
foreach ($a in $ConfigFile.line)
{
	if (!(
		$a -match "remove1" -or
		$a -match "remove2" -or
		$a -match "remove3" -or
		$a -match "remove4" -or
		$a -match "remove5" -or
		$a -match "remove6" -or
		$a -match "remove7" -or
		$a -match "remove8"))
	{
		$Needed = $a|%{$_ -replace ".*="}
		
		$Needed
		}
	
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Parsing ini file and adding it to a CSV file

Post by jvierra »

Code: Select all

$txt = @'
[Section_1]
not need =1
Primary_need1=something1
Primary_need2=something2
Primary_need3=something3
Primary_Recovery_needed4=something4
Primary_Recovery_needed5=something5
Primary_Recovery_needed6=something6
not need =2
not need =3
'@ -split "`n"

$data = $txt | Where-Object{ $_ -match 'Primary_' } | Out-String | ConvertFrom-StringData
$data.Primary_Recovery_needed5
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Parsing ini file and adding it to a CSV file

Post by sekou2331 »

Thanks for your help I have parsed the ini files as needed now i have an array of objects "System.Object[]". What I want to do is for every three objects in the array create a string. I tired the below but it is breaking it up multiple times.

Code: Select all

foreach ($i in $collected_parsed_items){
     "$($i[0]) $($i[1]) $($i[2])"
     }
    
example:
From:
user
IP
port
user2
IP2
port2
user3
IP3
port3

To:
user IP port
user2 IP2 port2
user3 IP3 port3
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Parsing ini file and adding it to a CSV file

Post by jvierra »

$i = 0
$a = 'one','two','three'
$a[$i..($i+2)] -join ' '
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Parsing ini file and adding it to a CSV file

Post by sekou2331 »

Thanks last question. Why am I only get one line back? it is not iterating through my array. Using the code below.

Code: Select all


$ConfigFile = sls -path .\file.ini -Pattern 'Primary', 'Secondary'  -SimpleMatch
$collected_parsed_items = @()
foreach ($a in $ConfigFile.line)
{
	if (!(
		$a -match "remove1" -or
		$a -match "remove2" -or
		$a -match "remove3" -or
		$a -match "remove4" -or
		$a -match "remove5" -or
		$a -match "remove6" -or
		$a -match "remove7" -or
		$a -match "remove8"))
	{
	 $parseOut_all = $Needed = $a|%{$_ -replace ".*="}
	 
		$collected_parsed_items += $parseOut_all
		
		
		}
	
}
$i=0
$collected_parsed_items[$i..($i+2)] -join '  '
 
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Parsing ini file and adding it to a CSV file

Post by jvierra »

Sorry but I cannot keep up with your continuous changes of code and a questions. Everything you need is in my answers you just need to stop changing my code without understanding how it works or you must provide an accurate example of the text and a clear question.

Using ConvertFrom-StringData will give you everything you need in object form.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Parsing ini file and adding it to a CSV file

Post by sekou2331 »

Sorry but I dont know how ConvertFrom-StringData is going to help me. If I am confusing you I am sorry. If you look at the code I am parsing an ini file by selecting on two strings Primary and Secondary. Then from there I am removing everything that is not needed. Then I am storing it in $collected_parsed_items which is now a System.Object[]. Now for every three items in the array I want to look like the below.

before:
a
b
c
e
f
d

after:
abc
efd

I think you gave me an idea and I will figure it out and share my results. Once again thank you and I am sorry for the confusion. Still trying to get better at scripting.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked