Skip to content

Instantly share code, notes, and snippets.

@mrozo
Created February 18, 2022 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrozo/444bc0d12bcd29f5048575edfdf05be1 to your computer and use it in GitHub Desktop.
Save mrozo/444bc0d12bcd29f5048575edfdf05be1 to your computer and use it in GitHub Desktop.
Multidimessiona sequence generator in bash. Sort of exrended seq
#!/usr/bin/env bash
help="
Multidimesionnal sequence
Generate every possible sequence consisting from n integer values, each from n-th range.
Range can be defined as:
* <k> - range from 1 to <k> inclusively
* <k>..<l> - range from <k> to <l> inclusively
* <k>..<l>..<m> -range from <k> to <m> with incremetn of <l> inclusively
Examples
$mseq 2 3
> 1 1
> 2 1
> 3 1
> 1 2
> 2 2
> 3 2
$ mseq 0..1 0..1
> 0 0
> 1 0
> 0 1
> 1 1
$ mseq 2 1 5..6
> 5 1 1
> 6 1 1
> 5 1 2
> 6 1 2
$ meseq 0..5..9 10..5..19
> 10 0
> 15 0
> 10 5
> 15 5
"
function mseq_internal() {
range="$(echo "$1" | tr -s '.' ' ')"
shift
pipe_to=cat
[[ "$#" > 0 ]] && pipe_to="mseq_internal $@"
while read line ; do
printf "%d $line\n" `seq -s' ' $range`
done | $pipe_to
}
function mseq() {
echo "" | mseq_internal $@
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]] ; then
mseq $@
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment