Tell me more ×  I asked a question regarding popping the last
line of a text file in PHP, and now, is it possible to re-write
the logic in shell script?  I tried this to obtain the last
line:  tail -n 1 my_log_file.log  but I am not sure how can I
remove the last line and save the file.  P.S. given that I use
Ubuntu server.  share|improve this question add comment  4
Answers  up vote 2 down vote accepted  (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 file LAST=$(tail -n 1 my_log_file.log)  #
truncate file let 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.
share|improve this answer Note that co