shell: delete the last line of a huge text log file [duplicate] shell: delete the last line of a huge text log file [duplicate] shell shell

shell: delete the last line of a huge text log file [duplicate]


To get the file content without the last line, you can use

head -n-1 logfile.log

(I am not sure this is supported everywhere)

or

sed '$d' logfile.log


What you want is truncate the file just before the last line without having to read the file entirely.

truncate -s -"$(tail -n1 file | wc -c)" file

That's assuming the file is not currently being written to.

truncate is part of the GNU coreutils (so generally found on recent Linux distributions) and is not a standardized Unix or POSIX command. Many "dd" implementations can be used to truncate a file as well.


(Solution is based on sch's answer so credit should go to him/her)

This approach will allow you to efficiently retrieve the last line of the file and truncate the file to remove that line. This can better deal with large inputs as the file is not read sequentially.

# retrieve last line from fileLAST=$(tail -n 1 my_log_file.log)# truncate filelet TRUNCATE_SIZE="${#LAST} + 1"truncate -s -"$TRUNCATE_SIZE" my_log_file.log# ... $LAST contains 'popped' last line

Note that this will not work as expected if the file is modified between the calls to tail and truncate.