Skip to content

Instantly share code, notes, and snippets.

@criztovyl
Last active August 29, 2015 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save criztovyl/d3164f38ad3c2ae28afd to your computer and use it in GitHub Desktop.
Save criztovyl/d3164f38ad3c2ae28afd to your computer and use it in GitHub Desktop.
Splits, sorts and joins a string by a separator
#!/bin/bash
# A Bash/Ruby script that splits, sorts and joins a string
# Copyright (C) 2015 Christoph "criztovyl" Schulz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
[ "$1" == "--help" ] || [ "$1" == "help" ] || [ "$1" == "-h" ] &&
{
echo "Usage: ./ssj SEPARATOR"
echo "Splits, sorts and joins a string."
exit
}
sep="$1"
[ ! "$sep" ] && {
mapfile -t ha <<< "`xclip -o | grep -o .`"
j=50
[ $j -lt ${#ha[@]} ] && echo ${ha[@]:0:$j} || echo ${ha[@]}
echo "Search delimiter, use a to navigate left and d do navigate right. Accept with return."
for ((i=0; i<${#ha[@]}; )); do
read -sn1 drct
case $drct in
"a")
[ $i -gt -${#ha[@]} ] && i=$((--i)) && do=1 || echo -ne "\r"$(tput el)"\rend"
;;
"d")
[ $((i+1)) -lt ${#ha[@]} ] && i=$((++i)) && do=1 || echo -ne "\r"$(tput el)"\rend"
;;
*)
[ ! "$drct" ] && char=${ha[$i]} && break || echo -ne "\r"$(tput el)"\rinvalid"
;;
esac
[ "$do" ] && echo -ne "\r"$(tput el)"\r${ha[$i]}, $i" && do=
char=${ha[$i]}
done
echo Char: "$char, pos $i"
sep="nil"
sep_from_string="if sep.nil?; sep=line[$i] end; ";
} || sep="'$sep'"
xclip -o | ruby -e "sep=$sep; \$stdin.each { |line| $sep_from_string puts line.split(sep).sort.join(sep)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment