1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#!/usr/bin/env bash
usage="\
Usage:
$(basename "$0") DIST_FILE
$(basename "$0") [--pretty] DIST_FILE SECTION_NAME
(1) Prints available sections/distributions for DIST_FILE.
(2) Extracts section SECTION_NAME from DIST_FILE.
"
if { [[ $# -ne 1 ]] && [[ $# -ne 2 ]] && [[ $# -ne 3 ]]; } || [[ "$1" =~ ^(-h|--help|--version)$ ]] ;then
echo -n "$usage" >&2
exit 1
fi
if [[ "$1" =~ ^--pretty$ ]]; then
dist_f="$2"
sect="$3"
else
dist_f="$1"
sect="$2"
fi
[[ -e "$dist_f" ]] || { ls -- "$dist_f"; exit 1; }
if [[ $# -ge 2 ]] ;then
section="$(echo "$sect" | grep -oE '\w+' | tr -d '\n')"
if ! grep --quiet "^BEGIN $section\\b" "$dist_f" ;then
echo "Error: Couldn't find section '$section' in '$dist_f'." >&1
exit 1
fi
if [[ "$1" =~ ^--pretty$ ]]; then
sed -nE "/^END $section($| )/ q; /^BEGIN $section($| )/,$ p" "$dist_f" | sed 1d | sed -nE "/^#/p"
sed -nE "/^END $section($| )/ q; /^BEGIN $section($| )/,$ p" "$dist_f" | sed 1d | grep -v "^#" | column -s " " -t
else
sed -nE "/^END $section($| )/ q; /^BEGIN $section($| )/,$ p" "$dist_f" | sed 1d
fi
else
grep ^BEGIN "$dist_f" | cut -c7-
fi
|