Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- params_or_stdin() { [ $# -eq 0 ] && cat || echo "$@"; }
- list_unroll_int() {
- tr -s ',' '\n' \
- | tr -s '-' ' ' \
- | while read -a L; do
- [ ${#L[@]} -eq 1 ] && echo ${L[@]} && continue
- echo ${L[@]} | xargs -rn2 seq
- done \
- | sort -n \
- | paste -sd ',' -
- }
- list_unroll() { params_or_stdin "$@" | list_unroll_int; }
- list_roll_int2() {
- local a b c d
- while read c; do
- [ -z "$a" ] && { a=$c; b=$c; }
- d=$[c-b]
- if [ $d -lt 2 ]; then
- [ $d -eq 1 ] && b=$c
- continue
- fi
- [ $[b-a] -gt 0 ] && echo "$a-$b" || echo "$a"
- a=$c; b=$c
- done
- [ $[b-a] -gt 0 ] && echo "$a-$b" || echo "$a"
- }
- list_roll_int() {
- tr -s ',' ' ' \
- | tr -s '[:space:]' '\n' \
- | uniq -u \
- | sort -n \
- | list_roll_int2 \
- | paste -sd ',' -
- }
- list_roll() { params_or_stdin "$@" | list_roll_int; }
- list_unroll 1-5,13,9
- #output: 1,2,3,4,5,9,13
- list_roll 1 2 3 4 6 10 8 14 11
- #output: 1-4,6,8,10-11,14
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement