I've a need to remove/truncate first N bytes from a log file while data is being recorded continuously. e.g. nohup.out
While I can use bash
truncate command like this.
truncate -c -s -10K my_file
.
This will truncate latest data from end of the file. So not useful in this case.
I need file to be truncated from beginning of file (that has older data) and preserve new one.
I checked online, Most of the example are using redirection or writing to temp file using dd
, head
etc. My need is to do this inline on the same file.
Closest match is sed
, but so far I found examples that truncates N characters from EVERY LINE.
e.g. below will delete 10 bytes from each line in my_file.
sed -i 's/^\(.\)\{10\}//g' my_file
I am looking for options where I can delete first N bytes starting with 1st line and ending on Kth line where Nth byte for deletion ends, thus preserving latest data at the bottom.
I can probably cook-up some logic to achieve this, but was wondering if there is "off the shelf" option available.
Any pointers? Thanks.