Created
November 16, 2021 17:48
-
-
Save grizmio/0d6ece0285cf769ec95b078d28e5a43c to your computer and use it in GitHub Desktop.
Remove last line from really big files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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