0

i have an issue that i just cant get working - I have to use VBSCRIPT for this.

I have a data that is being extracted from a log file but unfortunatly its not sorted post the extract (Im just parsing the log and using regex to grab spefic lines). My problem is that post this extration i am looking at the last 5 lines but the data extraction isnt sorted:

Sample Text Extraction:

2022-02-08,16:46:46.390,4,3812,Tag 7145
2022-02-08,16:46:46.390,4,3812,Tag 7145
2022-02-09,14:48:41.609,4,3460,Tag 22860
2022-02-09,14:48:50.609,4,872,Tag 22863
2022-02-09,14:48:59.156,4,3576,Tag 22866
2022-02-09,14:49:15.015,4,3932,Tag 22871
2022-02-09,14:49:39.218,4,4060,Tag 22877
2022-02-09,14:56:06.953,4,2440,Tag 22952
2022-02-08,08:34:55.703,4,3580,Tag 1347
2022-02-08,08:35:04.656,4,2124,Tag 1350
2022-02-08,08:35:14.609,4,2300,Tag 1353
2022-02-08,08:35:29.500,4,3996,Tag 1357
2022-02-08,08:35:35.296,4,2484,Tag 1359
2022-02-08,16:46:25.593,4,1648,Tag 7139
2022-02-09,14:49:06.968,4,2992,Tag 22868
2022-02-08,16:46:55.328,4,3236,Tag 7148
2022-02-08,16:47:04.953,4,3964,Tag 7151

I am using the following to get the last 5 lines:

ReDim buf(numlines-1)

For n = 0 To UBound(buf)
  buf(n) = Null
Next
i = 0
 Set f = oFSO.OpenTextFile(filename)
Do Until f.AtEndOfStream
  buf(i) = f.ReadLine
  i = (i+1) Mod numlines
Loop
f.Close

When i run this code it will give me the 5 last lines from the text file but not the correct ones. How can i sort the data in the array so that the extaction is correct (In this case i should get the 9th Feb data and not the 8th)

Rory
  • 21
  • 4
  • 1
    Feels like you are making this way more complicated than needs to be. Looping the buf to fill them with Null, calculating the i using Mod numlines... – Geert Bellekens Feb 10 '22 at 07:22
  • And what did you get when using command line `Type Test.txt | sort>Test_sorted.txt` ? – Hackoo Feb 10 '22 at 09:18
  • You can use Powershell in one line `Powershell -C "GC '.\test.txt' | sort | select -Last 5 | Out-File .\TestLast5Lines_sorted.txt"` – Hackoo Feb 10 '22 at 10:23
  • A lot of how you sort it depends on what value you want to sort on, looking at the data there are at least 5 columns you could sort on. You could split out the values into a disconnected `ADODB.Recordset` and sort using the in-built functions, or use an `ArrayList` see [Natural Sorting using VB script](https://stackoverflow.com/a/37937917). – user692942 Feb 10 '22 at 10:25

0 Answers0