Set of data needs to be rearranged and exported as CSV - powershell

I have a set of data and it needs to be rearranged with header. PowerShell is a quick way, I think, to do this.
The data as an example:
AAA,
123,53,145,
text1 text12,text2,23.00,
21,6,0.21,0.00,2,
1,321,4
BBB,1,421,5,
text3 text34,
text4,41,22.5,1,
23,674,75,8,5....
As you can see the ',' seperates the values and the dot is just part of the value. The AAA and BBB represent the names and the values for AAA are before BBB. I expect the values to be like this within CSV file with each value in a new cell:
Name: AAA
abc: 123
def: 53
ghi: 145
jkl: text1 text12
mno: text2
pqr: 23.00
stu: 21
vwx: 6
yz: 0
Or the header would be like: Name abc def etc. and the values in the next cell under the header.
In total there are 16 values separated by a comma for one name (ex. AAA). There are maybe hundreds of names with random values.
I didn't get the result with Sort-Object, because it would scan the data and put the values in random order.

here's one way to do the job. it presumes that there are 18 properties and that each object ends with a line that does NOT end with a comma.
the resulting collection can be gracefully sent to a CSV via Export-CSV. [grin]
# fake reading in a text file as one multi-line string
# in real life, use Get-Content -Raw
# similar names & properties are NOT part of the data
# they are there for visually crosschecking the output
$InStuff = #'
Object_A,
A1,A2,A3,
A4 some other stuff,A5,
A6,A7 more extras,A8,A9,A10.00,
A11,A12,A13 thirteen,A14,
A15,A16.666,A17,A18
Object_B,B1.1,B2.2,B3.3,
B4 some other stuff,B5,B6,B7 more extras,B8,B9,B10.00,
B11,B12,B13 thirteen,B14,
B15,B16.666,B17,B18
'#
$ColumnHeaders = ('Name' +
' Col_01 Col_02 Col_03 Col_04 Col_05 Col_06 Col_07 Col_08 Col_09 Col_10' +
' Col_11 Col_12 Col_13 Col_14 Col_15 Col_16 Col_17 Col_18') -split ' '
$CSV = $InStuff -split "(?<!,)$([environment]::NewLine)" |
ForEach-Object {
$_ -replace ",$([environment]::NewLine)", ','
}
$Results = $CSV |
ConvertFrom-Csv -Header $ColumnHeaders
$Results
output ...
Name : Object_A
Col_01 : A1
Col_02 : A2
Col_03 : A3
Col_04 : A4 some other stuff
Col_05 : A5
Col_06 : A6
Col_07 : A7 more extras
Col_08 : A8
Col_09 : A9
Col_10 : A10.00
Col_11 : A11
Col_12 : A12
Col_13 : A13 thirteen
Col_14 : A14
Col_15 : A15
Col_16 : A16.666
Col_17 : A17
Col_18 : A18
Name : Object_B
Col_01 : B1.1
Col_02 : B2.2
Col_03 : B3.3
Col_04 : B4 some other stuff
Col_05 : B5
Col_06 : B6
Col_07 : B7 more extras
Col_08 : B8
Col_09 : B9
Col_10 : B10.00
Col_11 : B11
Col_12 : B12
Col_13 : B13 thirteen
Col_14 : B14
Col_15 : B15
Col_16 : B16.666
Col_17 : B17
Col_18 : B18

Related

How to find specific string, with certain starting and ending character in Powershell

how to find all the strings which starts with open square bracket and ends with close square bracket in text file.
used below code but getting some unwanted contents also. How to avoid those unwanted contents
Select-String -Path "text.txt" -Pattern '\['
input file:
some conents [hello]
adfhjadf adfjkkf [ghad]
[check] dfgsf sfgfs
required output file
[hello]
[ghad]
[check]
Here, try this:
$string = #'
some conents [hello]
adfhjadf adfjkkf [ghad]
[check] dfgsf sfgfs
'#
([regex]'\[.*?\]').Matches($string)
Note, this will match anything inside the brackets. If you're looking only for English characters you should refine the regex.
Output should look like this:
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 13
Length : 7
Value : [hello]
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 39
Length : 6
Value : [ghad]
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 47
Length : 7
Value : [check]
If you just want to see the matches you can do:
PS /> ([regex]'\[.*?\]').Matches($string).Value
[hello]
[ghad]
[check]

Using FromArgb for Int32[] value results in swapped Red and Blue values

I have this Int32[] value 14508153 which should be:
R : 121
G : 96
B : 221
If I use:
[System.Drawing.Color]::FromArgb(14508153)
It returns:
R : 221
G : 96
B : 121
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : dd6079
Questions
How or why are those values swapped for R and B using that function?
Is there a built-in PowerShell method to convert them correctly?
I can't speak to why the bytes need to be rearranged (see the bottom section for thoughts), but here's how you can do it:
Add-Type -AssemblyName System.Drawing
$bytes = [System.BitConverter]::GetBytes(14508153)
[byte[]] $rearrangedBytes = $bytes[2], $bytes[1], $bytes[0], $bytes[3]
[System.Drawing.Color]::FromArgb(
[System.BitConverter]::ToInt32($rearrangedBytes, 0)
)
See System.BitConverter.GetBytes(), System.BitConverter.ToInt32().
The above yields:
R : 121
G : 96
B : 221
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : 7960dd
It appears that only 3 of the bytes in your [int] (System.Int32) value are relevant, and that they are in Big-Endian order (see the Wikipedia article about endianness).
By contrast, System.Drawing.Color.FromArgb() expects all 4 bytes to be relevant, in Little-Endian order.

stockage in structure and iteration

I would like to understand one thing :
When I write : Propreties.Device.Time = Data.Device(lobo(:,2),1) - Data.Device(lobo(:,1),1) I obtain a stockage of all the differencies (it's great)
But when I write : Propreties.Device.Prop = sum(Data.Device(lobo(:,1) : lobo(:,2)),2)*dt I don't obtain a stockage of all results (that I would like to obtain as previous exemple) but an overwrite of each of them so at the end I have only one value :-/
Could someone explain to me what differs between the two examples and what I could implement to obtain in the second example the same type of result as in the first (ie a list of results and not a single result) (without making a loop) ?
(Matlab verison : R2017a)
Some data for exemple :
Data.Device = [1.86000000000000 675 0;1.87000000000000 685 0;1.88000000000000 695 0;1.89000000000000 705 0;1.90000000000000 710 5;1.91000000000000 715 50;1.92000000000000 700 120;1.93000000000000 685 180;1.94000000000000 655 235;1.95000000000000 620 285;1.96000000000000 565 305;1.97000000000000 505 315;1.98000000000000 435 335;1.99000000000000 360 345;2 285 355];
lobo = [1 5; 6 15];
dt = 0.01
Propreties.Device.Time = Data.Device(lobo(:,2),1) - Data.Device(lobo(:,1),1);
Propreties.Device.Prop = sum(Data.Device(lobo(:,1) : lobo(:,2)),2)*dt

delete rows with character in cell array

I need some basic help. I have a cell array:
TITLE 13122423
NAME Bob
PROVIDER James
and many more rows with text...
234 456 234 345
324 346 234 345
344 454 462 435
and many MANY (>4000) more with only numbers
text
text
and more text and mixed entries
Now what I want is to delete all the rows where the first column contain a character, and end up with only those rows containing numbers. Row 44 - 46 in this example.
I tried to use
rawdataTruncated(strncmp(rawdataTruncated(:, 1), 'A', 1), :) = [];
but then i need to go throught the whole alphabet, right?
Given data of the form:
C = {'FIRSTX' '350.0000' '' '' ; ...
'350.0000' '0.226885' '254.409' '0.755055'; ...
'349.9500' '0.214335' '254.41' '0.755073'; ...
'250.0000' 'LASTX' '' '' };
You can remove any row that has character strings containing letters using isstrprop, cellfun, and any like so:
index = ~any(cellfun(#any, isstrprop(C, 'alpha')), 2);
C = C(index, :)
C =
2×4 cell array
'350.0000' '0.226885' '254.409' '0.755055'
'349.9500' '0.214335' '254.41' '0.755073'

How to replace a value with "." in sed

I want to replace all instances of "a number followed by any number of spaces followed by a period and possibly more spaces" with the number and period only.
For example, '14 . x' will become '14.x'.
My test data is:
1. c4 e5 2. g3 c6 { good move. } 3. Bg2 Nf6 4. Nc3 $6 d5 5. cxd5 cxd5 6. Qb3 Nc6 $1.. Nxd5 Nd4
8. Nxf6+ Qxf6 9. Qd1.f5 10. d3 Rc8 (10... Bb4+ $5 11. Bd2 Bxd2+ 12. Qxd2 Qa6 $1.3. Rc1.xa2
14. Bxb7 $2 Rb8 15. Qb4 Bd7) 11. Kf1.c5 12. Nf3 O-O
How can I do that?
If you want any number of spaces removed from either side of the period, you should try s/\([0-9]\) *\. */\1./g:
$ echo '11. A 12 .B 13 . C 14.D 15 . E' | sed 's/\([0-9]\) *\. */\1./g'
11.A 12.B 13.C 14.D 15.E
For your test data, the results are:
1.c4 e5 2.g3 c6 { good move. } 3.Bg2 Nf6 4.Nc3 $6 d5 5.cxd5 cxd5 6.Qb3 Nc6 $1.. Nxd5 Nd4
8.Nxf6+ Qxf6 9.Qd1.f5 10.d3 Rc8 (10... Bb4+ $5 11.Bd2 Bxd2+ 12.Qxd2 Qa6 $1.3.Rc1.xa2
14.Bxb7 $2 Rb8 15.Qb4 Bd7) 11.Kf1.c5 12.Nf3 O-O