Reputation: 13
first of all, i´m sorry for my bad english
i´m completely new to powershell and not so into programming, but i´m willing to learn
So my problem is:
I got a .conf data with textes like this:
[XYZ1 - XYZXYZ, XYZXYZ]
room a201*,f110
XYZXYZXYZ
XYZXYZXYZ
XYZXYZXYZ
XYZXYZXYZ
[XYZ2 - XYZXYZ, XYZXYZ]
room a202*,f110
XYZXYZXYZ
XYZXYZXYZ
XYZXYZXYZ
My goal is to export the text into a csv or other data like this:
a201,[XYZ1 - XYZXYZ, XYZXYZ]
f110,[XYZ1 - XYZXYZ, XYZXYZ]
a202,[XYZ1 - XYZXYZ, XYZXYZ]
f110,[XYZ1 - XYZXYZ, XYZXYZ]
So for each text with the [] brackets, it should take the room (line under) and check, if there is another room (a201,f110), remove the word "room" and the other lines under and the star "*" and export it like above.
My first step was something like that:
Select String -input C:\xyz\text.conf -pattern "\[*\]","room *" | select line
Then i tried to remove the word "room" with Substring, but that didnt work, and i have no idea how to make a loop for each room and export that into 2 columns.
I hope you guys can help me.
Greets Marcel
Upvotes: 1
Views: 141
Reputation: 58441
Following might be one solution. This uses the Context
property of Select-String
to retain the previous line where you can then split and replace the searched line to get the desired output.
Select-String -input C:\xyz\text.conf -pattern 'room' -Context 1,0 | % {
$current = $_
$current.Line -split ',' | % {
"$($_ -replace 'room ' -replace '\*'),$($current.Context.PreContext)"
}
}
Test script
("[XYZ1 - XYZXYZ, XYZXYZ]",
"room a201*,f110",
"XYZXYZXYZ",
"XYZXYZXYZ",
"XYZXYZXYZ",
"XYZXYZXYZ",
"",
"[XYZ2 - XYZXYZ, XYZXYZ]",
"room a202*,f110",
"XYZXYZXYZ",
"XYZXYZXYZ",
"XYZXYZXYZ") |
sls 'room' -Context 1,0 | % {
$current = $_
$current.Line -split ',' | % {"$($_ -replace 'room ' -replace '\*'),$($current.Context.PreContext)"}
}
Output
a201,[XYZ1 - XYZXYZ, XYZXYZ]
f110,[XYZ1 - XYZXYZ, XYZXYZ]
a202,[XYZ2 - XYZXYZ, XYZXYZ]
f110,[XYZ2 - XYZXYZ, XYZXYZ]
Upvotes: 1