I have large number of bz2 formatted files (30GB each) without any header. I can split them easily in 500M in each size with the following pileline.
bzcat logging.abc_gps.bz2 | pv | split -b 500M -d -a 4 --filter='bzip > $FILE.csv.bz2' - splitted_file-
But I cannot add the header ['a' 'b' 'c' 'd' 'e' 'f' 'timestamp'] that I want to include each of the splitted bz2 file.
More importantly I want to split the file not based on the 500M, rather I want to split the bz2 file per day (example: splitted_file_2021-01-01.csv.bz2 and splitted_file_2021-01-02.csv.bz2)based on the content of timestamp in the data.
Data is tab-delimited text, like below (no header, need to add them):
19252547212 1 3041 2 1 74.18 1.8504 2021-05-01 00:00:00
19252547213 1 5055 2 1 0 0 2021-05-01 00:00:00
19252547214 1 5073 1 1 53.81 0.1836 2021-05-01 00:00:00
You can use the
bz2package to open BZ2 encoded files and treat them as regular file objects. There is a minor performance advantage to read / write in binary. Assuming your data is either ASCII or UTF-8 and no tab characters need to be escaped in the data, you can just read the file line by line, opening and writing the outputs as new timestamps appear.You may be able to speed this up with a pipeline. Give both the decryption and encryption to separate bzip2 processes that will run in parallel on different cores. Instead of a shell pipeline, you can create pipes and files to do it in the script itself. Assuming
bzip2exists on your system you could do the following. I added thetqdmmodule to print progress along the way.