On Thu, Mar 26, 2020 at 07:59:38AM -0700, Joe Clark wrote:
> I am looking for a way to periodically remove the first N lines of a file,
> to implement a "quick and dirty" log file size management scheme.
>
> I have found that vi will effectively do this, and (somewhat surprisingly)
> the logging application seems to be okay with its log file being modified.
This is a very late response, but thought it might be good for the
archive in case someone was searching.
Several responders to the OP suggested sed or tail, which the OP didn't
want because he needed an in-place change that *didn't change the
inode*.
This is a good fit for... 'ed'! No, not me, ed the line editor.
$ seq 1000 > /tmp/nums.txt
$ wc -l /tmp/nums.txt
1000 /tmp/nums.txt
$ stat --printf "%i\n" /tmp/nums.txt
285890
$ printf '1,500d\nw\n' | ed /tmp/nums.txt
3893
2001
$ wc -l /tmp/nums.txt
500 /tmp/nums.txt
$ head -n 3 /tmp/nums.txt
501
502
503
$ tail -n 3 /tmp/nums.txt
998
999
1000
$ stat --printf "%i\n" /tmp/nums.txt
285890
You can see that ed is scriptable in a limited way (no loops or branches), but unlike vim doesn't assume interactive input, and saves in-place.
--
Ed Blackman