I have a file that has ~12300000
rows of type <timestamp, reading>
–
1674587549.228 29214
1674587549.226 29384
1674587549.226 27813
1674587549.226 28403
1674587549.228 28445
...
1674587948.998 121
1674587948.998 126
1674587948.999 119
1674587949.000 126
1674587948.996 156
1674587948.997 152
1674587948.998 156
1674588149.225 316
1674588149.226 310
1674588149.223 150
1674588149.224 152
1674588149.225 150
1674588149.225 144
...
1674588149.225 227
1674588149.226 233
1674588149.226 275
The last - first
timestamp equals 600
. I want to create a new file that starts with row last - nth
timestamp till the end.
For example, if n=200
, the new file should start with 1674588149.226-200
i.e. from 1674587949.000 126
to 1674588149.226 275
.
Can this be done using a linux command / shell script? If so how it can be done? Thanks.
2
Answers
If I understood correctly, you are trying to create files which have a constant and equal number of lines in each, starting from the last one.
If so, this script will perform the task.
If you only want one file, then you can remove the logic associated with the looping and index value iterations.
Note: The name of each file corresponds to the first part of the last line in each file (i.e. last entry of record).
This example does splitting for groupings of 5 lines. You can replace the 5 by 100 or 200, as you see fit.
I you only want the last 200 line entries of a log, then the absolute simplest is by using tail. Namely
If you want to create multiple files of 200 lines each, you could use the sequence
in a loop that include assigning a unique name for each slice ${newLogName} slice.
OR, you could create a reverse log at the outset, and create the sublists working down the reverse list, but remembering to reverse each individual shortlist before saving those in their final form.