Skip to content

Instantly share code, notes, and snippets.

@meoow
Last active August 29, 2015 14:08
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 meoow/a78ae53fc75598bf0708 to your computer and use it in GitHub Desktop.
Save meoow/a78ae53fc75598bf0708 to your computer and use it in GitHub Desktop.
Wrappers for CUT and SORT treat consecutive whitespaces as field separator
cuttor() {
local cutopts files
declare -a cutopts
declare -a files
while [[ $# -gt 0 ]];do
case "$1" in
-f) shift; cutopts+=(-f "$1") ;;
-f?*) cutopts+=("$1") ;;
*) files+=("$1") ;;
esac
shift
done
sed 's/^[[:space:]]*//;
s/[[:space:]]*$//;
s/[[:space:]][[:space:]]*/'$'\x1f''/g' "${files[@]}" |
cut -d$'\x1f' "${cutopts[@]}" |
tr $'\x1f' "${ORS:-$'\t'}"
}
sorttor() {
local sortopts files k
declare -a sortopts
declare -a files
while [[ $# -ne 0 ]];do
case "$1" in
-b|-d|-f|-g|-i|-M|-n|-r|-c|-m|-s|-u|--ignore-leading-blanks|--dictionary-order|--ignore-case|--general-numeric-sort|--ignore-nonprinting|--month-sort|--numeric-sort|--reverse|--check|--merge|--stable|--unique|-k?*|-o?*|-S?*|-T?*|--key=?*|--output=?*|--buffer-size=?*|--temporary-directory=?*)
sortopts+=("$1")
;;
-k|-o|-S|-T|--key|--output|--buffer-size|--temporary-directory)
k=$1
shift
sortopts+=($k "$1")
;;
-d) shift ;;
-d?*) : ;;
--help) sort --help; return 0 ;;
*) files+=("$1") ;;
esac
shift
done
sed 's/^[[:space:]]*//;s/[[:space:]]*$//;
s/[[:space:]][[:space:]]*/'$'\x1f''/g' "${files[@]}" |
sort -t$'\x1f' "${sortopts[@]}" |
tr $'\x1f' "${ORS:-$'\t'}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment