Skip to content

Instantly share code, notes, and snippets.

@yyuu
Created March 24, 2017 04:12
Show Gist options
  • Save yyuu/007f05392344037882d0f819313d2f97 to your computer and use it in GitHub Desktop.
Save yyuu/007f05392344037882d0f819313d2f97 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# slice.sh - split stdin lines into chunks and run given commands
#
# Examples: Run given commands once per 2 lines
#
# ```sh
# { echo foo
# echo bar
# echo baz
# } | ./slice.sh -n2 sh -c 'echo $$; cat'
# 94245
# foo
# bar
# 94251
# baz
# ```
#
set -e -o pipefail
n=1
while [ "$#" -gt 0 ]; do
case "$1" in
"-n"* )
if [[ "$1" == "-n" ]]; then
n="$2"
shift 1
else
n="${1#-n}"
fi
;;
* )
break
;;
esac
shift 1
done
TMPDIR="$(mktemp -d "${TMP:-/tmp}/${0##*/}.XXXXXXXX")"
on_exit() { rm -fr "${TMPDIR}"; }
trap on_exit EXIT
cat > "${TMPDIR}/stdin"
total="$(cat "${TMPDIR}/stdin" | wc -l)"
for slice in $(seq 1 "${n}" "${total}"); do
cat "${TMPDIR}/stdin" | tail -n "+${slice}" | head -n "${n}" > "${TMPDIR}/slice${slice}"
cat "${TMPDIR}/slice${slice}" | "$@"
done
# vim:set ft=sh :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment