Is it possible to keep only the last 10 lines of a lines with a simple shell command?
tail -n 10 test.log
delivers the right result, but I don't know how to modify test.log itself. And
tail -n 10 test.log > test.log
doesn't work.
Is it possible to keep only the last 10 lines of a lines with a simple shell command?
tail -n 10 test.log
delivers the right result, but I don't know how to modify test.log itself. And
tail -n 10 test.log > test.log
doesn't work.
Invoke ed command (text editor):
This will send command to delete lines ('1,-10d'), save file ('w') and exit ('q').
Also note that ed fails (return code is 1) when the input file has less than 11 lines.
Edit: You can also use vi editor (or ex command):
But if the input file has 10 or less lines vi editor will stay opened and you must type ':q' to exit (or 'q' with ex command).
Also you may use a variable:
You can do it using tempfile.
Quotes are important. They preserve newline characters.