Last active
December 10, 2015 23:48
-
-
Save mynameisfiber/4511770 to your computer and use it in GitHub Desktop.
Split a gzip'ed newline separated file into multiple files by line count.
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 | |
| file="$1"; | |
| numlines="$2" | |
| basefile=${file%.gz} | |
| isMore=1 | |
| function write_n_lines { | |
| local c=0; | |
| while [[ "$c" -lt "$1" ]]; do | |
| if ! read LINE; then | |
| return 0; | |
| fi; | |
| echo "$LINE"; | |
| c=$(( $c + 1 )); | |
| done; | |
| return 1; | |
| } | |
| gunzip -c "$file" | pv -rbt | ( | |
| fn=0; | |
| while [[ $isMore -eq 1 ]]; do | |
| newfile=$( printf "%s-%04d.gz" "$basefile" $fn ); | |
| write_n_lines $numlines | pigz -c > $newfile | |
| isMore=${PIPESTATUS[0]} | |
| fn=$(( $fn + 1 )) | |
| done; | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment