Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Created August 25, 2017 11:19
Show Gist options
  • Save wookietreiber/600a4f9ca3433641092c9e6cb0668196 to your computer and use it in GitHub Desktop.
Save wookietreiber/600a4f9ca3433641092c9e6cb0668196 to your computer and use it in GitHub Desktop.
split a file into pieces but keep a common header of first n lines
#!/bin/bash
export SPLIT_header_n=$1
target_size=$2
export SPLIT_input_file=$3
target_wo_header=$(( "$target_size" - "$SPLIT_header_n" ))
[[ -n $SPLIT_input_file ]] || {
echo "usage: $(basename "$0") SPLIT_header_n target_n SPLIT_input_file" >&2
exit 1
}
function my_header {
head -n "$SPLIT_header_n" "$SPLIT_input_file"
cat
}
export -f my_header
sed "1,${SPLIT_header_n}d" "$SPLIT_input_file" |
split \
-d \
-l "$target_wo_header" \
--filter='my_header > $FILE' \
- "$(basename "$SPLIT_input_file")."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment