Concatenate multiple lines from a text file

I have a text file that lists data on multiple lines. I can write relevance to get one or the other but not both at the same time

lines whose (it contains “Disk”) of file “C:\Program Files\Dell\SysMgt\idrac\dskserialdump.txt”

following texts of firsts "= " of lines whose (it contains “SerialNumber”) of file “C:\Program Files\Dell\SysMgt\idrac\dskserialdump.txt”.

See below is the format it lists in a text file in raw format

Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1
SerialNumber = XXXXXXXXXXXXX
Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1
SerialNumber = XXXXXXXXXXXXX
Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1
SerialNumber = XXXXXXXXXXXXX

I want to parse the file in this format. The X’s being the serial number.

Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX
Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX
Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1 XXXXXXXXXXXXX

@jlpeppers, I’m still thinking this one through, but the following relevance queries:

concatenation “|” of lines whose (it contains “Disk”) of file “C:\Program Files\Dell\SysMgt\idrac\dskserialdump.txt”

concatenation “|” of following texts of firsts "= " of lines whose (it contains “SerialNumber”) of file “C:\Program Files\Dell\SysMgt\idrac\dskserialdump.txt”

Will return the two distinct datasets:

Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1|Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1|Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1

XXXXXXXXXXXXX|XXXXXXXXXXXXX|XXXXXXXXXXXXX

Which can then be split and then concatenated accordingly in your Action Script.

I’m going to keep playing with the relevance and will re-post if I come up with a better approach.

2 Likes

I happened to have a structure for this I had used for something else. So, assuming the lines of the file are as following:

Q: (lines of file “C:\Program Files\Dell\SysMgt\idrac\diskserialdump.txt” as string)
A: Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1
A: SerialNumber = XXXXXXXXXXXXX
A: Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1
A: SerialNumber = XXXXXXXXXXXXX
A: Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1
A: SerialNumber = XXXXXXXXXXXXX

Q: (preceding text of first “||” of it, following text of first " = " of following text of first “||” of it) of ((following texts whose (number of substrings “||” of it is 1) of substrings “||” of preceding texts whose (number of substrings “||” of it mod 2 is 0) of substrings “||” of it) of (“||” & (concatenation “||” of (lines of file “C:\Program Files\Dell\SysMgt\idrac\diskserialdump.txt” as string)) & “||”))
A: Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX
A: Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX
A: Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX

2 Likes

If the lines are guaranteed to always be in that exact order, next line of <line> and previous line of <line> could also be used.

q: lines of file "c:\temp\test.txt"
A: Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1
A: SerialNumber = XXXXXXXXXXXXX
A: Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1
A: SerialNumber = YYYYYYYYYYYYYY
A: Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1
A: SerialNumber = ZZZZZZZZZZZZZZ
T: 2.188 ms
I: plural file line

q: (it, next lines of it) of lines starting with "Disk" of file "c:\temp\test.txt"
A: Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1, SerialNumber = XXXXXXXXXXXXX
A: Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1, SerialNumber = YYYYYYYYYYYYYY
A: Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1, SerialNumber = ZZZZZZZZZZZZZZ
T: 1.756 ms
I: plural ( file line, file line )

q: (it, following texts of firsts "SerialNumber = " of next lines of it) of lines starting with "Disk" of file "c:\temp\test.txt"
A: Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX
A: Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1, YYYYYYYYYYYYYY
A: Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1, ZZZZZZZZZZZZZZ
T: 0.947 ms
I: plural ( file line, substring )
4 Likes

Thanks, this helped tremendously.

If the lines are not in a guaranteed order where I can pull other data from the list what inspectors could I use other than next line of…?

ProductId
Revision
SerialNumber
PartNumber
NegotiatedSpeed

This may be inefficient but it works by daisy chaining the next line statements…

((it, following texts of firsts "= " of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next line of next lines of it) of lines starting with “Disk” of file “C:\Program Files\Dell\SysMgt\idrac\dskserialdump.txt”)

If you could give a sample of your “C:\Program Files\Dell\SysMgt\idrac\dskserialdump.txt” file, we might be able to do some parsing tricks for this, even when lines are not in a predictable order.

1 Like

Capture

Here is a sample of the text file. When using BES Reports to export the data it puts the data in a single cell. Is there a way to add a semicolon in between the entries where it can be delimited?

This is the relevance I’m using to parse the text file

if exists file “C:\Program Files\Dell\SysMgt\idrac\dskdump.txt” then ((it, following texts of firsts "= " of next lines of it) of lines starting with “Disk” of file “C:\Program Files\Dell\SysMgt\idrac\dskdump.txt”) else if exists file “C:\Program Files\Dell\SysMgt\OM_iDRACTools\racadm\dskdump.txt” then ((it, following texts of firsts "= " of next lines of it) of lines starting with “Disk” of file “C:\Program Files\Dell\SysMgt\OM_iDRACTools\racadm\dskdump.txt”) else nothing

@jlpeppers

Give this one a try to see if it meets your requirement: It should take all the lines and put them together with semicolon between.

if exists file "C:\Program Files\Dell\SysMgt\idrac\dskdump.txt" then (concatenation ";" of (it as string) of (it, following texts of firsts "= " of next lines of it) of lines starting with "Disk" of file "C:\Program Files\Dell\SysMgt\idrac\dskdump.txt") else if exists file "C:\Program Files\Dell\SysMgt\OM_iDRACTools\racadm\dskdump.txt" then (concatenation ";" of (it as string) of (it, following texts of firsts "= " of next lines of it) of lines starting with "Disk" of file "C:\Program Files\Dell\SysMgt\OM_iDRACTools\racadm\dskdump.txt") else nothing

it works in fixlet debugger but gives in the console

Not sure what you mean. Perhaps a screenshot will help?

Capture

This is what I see. If I run the query in fixlet debugger I get results.

ok - will go full “error trapping” mode…

q: if number of folders ("C:\Program Files\Dell\SysMgt\idrac";"C:\Program Files\Dell\SysMgt\OM_iDRACTools\racadm") = 1 then (if (number of files "dskdump.txt" of folders("C:\Program Files\Dell\SysMgt\idrac";"C:\Program Files\Dell\SysMgt\OM_iDRACTools\racadm") = 1) then (concatenation ";" of (it as string) of (it, following texts of firsts "= " of next lines of it) of lines starting with "Disk" of files "dskdump.txt" of folders("C:\Program Files\Dell\SysMgt\idrac";"C:\Program Files\Dell\SysMgt\OM_iDRACTools\racadm")) else ("missing or many files")|error "something wrong at file level") else ("missing or multiple folders")|error "something wrong at folder level"
A: Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX;Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX;Disk.Bay.2:Enclosure.Internal.0-1:RAID.Integrated.1-1, XXXXXXXXXXXXX

The parameters of the file are differing per server meaning the order in which they are listed are not consistent across all servers which gives incorrect information. In this example the server listed in this order below but on another Serialnumber maybe at the top of the list. I’m not having any control how it is written to the text file. How would I be able to extract each parameter correctly?

Disk.Bay.0:Enclosure.Internal.0-1:RAID.Integrated.1-1
Size = 136.13 GB
Manufacturer = SEAGATE
SerialNumber = XXXXXXXXX
PartNumber = YYYYYYYYY
Disk.Bay.1:Enclosure.Internal.0-1:RAID.Integrated.1-1
Size = 136.13 GB
Manufacturer = SEAGATE
SerialNumber = ZZZZZZZZZZ
PartNumber = WWWWWWW

That is indeed a challenging question – so much so, that we previously had a Challenge on just such a case!

Depending on how this data is being retrieved,it may be easier to change the structure of the data to something more easily parsed, but if you truly have no control over it and cannot get these results from WMI instead, we had four successful approaches on the Challenge at Relevance Challenge December 2019 BONUS: Parsing Paragraphs (answer provided)

1 Like