Skip to content

Instantly share code, notes, and snippets.

@grizmio
Created November 16, 2021 17:48
Show Gist options
  • Select an option

  • Save grizmio/0d6ece0285cf769ec95b078d28e5a43c to your computer and use it in GitHub Desktop.

Select an option

Save grizmio/0d6ece0285cf769ec95b078d28e5a43c to your computer and use it in GitHub Desktop.
Remove last line from really big files
#!/bin/bash
# credits:
# https://stackoverflow.com/a/17794626/16157358
# https://stackoverflow.com/questions/4881930/remove-the-last-line-from-a-file-in-bash
### NOT DONE BY ME, JUST KEEPING FOR POSTERITY
filename="example.txt"
file_size="$(stat --format=%s "$filename")"
trim_count="$(tail -n1 "$filename" | wc -c)"
end_position="$(echo "$file_size - $trim_count" | bc)"
dd if=/dev/null of="$filename" bs=1 seek="$end_position"
Or alternatively, as a one liner:
dd if=/dev/null of=<filename> bs=1 seek=$(echo $(stat --format=%s <filename> ) - $( tail -n1 <filename> | wc -c) | bc )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment