Skip to main content
Add method to remove the last n lines
Source Link
h3rrmiller
  • 13.5k
  • 5
  • 34
  • 42

in sed $ is the last line so to delete the last line:

sed '$d' <file>

Updated to include a way to delete multiple lines from the bottom of the file:

tac <file> | tail -n +3 | tac > <new_file>
  • tac reads the file backwards (cat backwards)
  • tail -n +3 reads the input starting at the nth line
  • tac reads the input and reverses the order back to the original

in sed $ is the last line so to delete the last line:

sed '$d' <file>

in sed $ is the last line so to delete the last line:

sed '$d' <file>

Updated to include a way to delete multiple lines from the bottom of the file:

tac <file> | tail -n +3 | tac > <new_file>
  • tac reads the file backwards (cat backwards)
  • tail -n +3 reads the input starting at the nth line
  • tac reads the input and reverses the order back to the original
deleted 3 characters in body
Source Link
h3rrmiller
  • 13.5k
  • 5
  • 34
  • 42

in sed $ is the last line so to delete the last line:

sed -e '$d' <file>

in sed $ is the last line so to delete the last line:

sed -e '$d' <file>

in sed $ is the last line so to delete the last line:

sed '$d' <file>
Source Link
h3rrmiller
  • 13.5k
  • 5
  • 34
  • 42

in sed $ is the last line so to delete the last line:

sed -e '$d' <file>