#!/bin/sh cat << 'EEE' > /dev/null /* ssort .... sort command parser, bourne-shell script * Copyright (C) 2018,2019 Momi-g * * 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/>. */ EEE #optcheck-------- if [ "$1" = "-h" ] ; then cat << 'EEE' HowTo (ssort, simple sort command parser) option: -h (this) ...and 'sort' command option ------ ex) echo " q 12 AAA w 03 BBB e 5 CCC t 3 BBB y 03 ZZZ " | ssort 2n,3r,1 # parse 'sort -b -k 2,2n -k 3,3r -k 1,1' >>> y 03 ZZZ t 3 BBB w 03 BBB e 5 CCC q 12 AAA ex) ... | ssort 1n -o out.csv #... $1 must be a sequence string. 2,3nr,1: sort sequence. second field - third f - first f. n or r etc: sort option. see 'sort --help'. n:num r:reverse. o,M,f,V... etc. ...99.9% users only use n/r option. EEE exit 1 fi buf=`echo $1 | awk '$1 ~ /^-/{print $0}' ` if [ "$buf" != "" ] || [ $# -eq 0 ] ; then echo "$0: args err. see -h. sleep." >/dev/stderr sleep 1000 exit 1 fi # https://abicky.net/2011/07/24/174632/ # # ãµã¤ãƒ¼ã«æ›¸ã„ãŸã‚‰sort -k 1 -k 2n -k 3n # ãŒåˆ†ã‹ã‚Šã‚„ã™ã„ãŒã€ã“ã‚Œã¯äºˆæƒ³é€šã‚Šã«åƒã‹ãªã„。 # sort -k 1-3 -k 2-3n -k 3-3n # ã¿ãŸã„ã«ä»¥é™ã®ã‚ー全ã¦ã‚’対象ã¨ã—ã¦ã—ã¾ã†ãŸã‚。 # ã¤ã„ã§ã«æ–‡å—ã®ä½ç½®æŒ‡å®šã‚‚çœç•¥ã•ã‚Œã¦ã‚‹ã€‚ # 1.1ã¯1ã¤ã‚ã®ã‚ーã®1æ–‡å—目をæ„味ã™ã‚‹ã€‚ # # sort -k 1.1,1.$ -k 2.2,2.$n -k 3.2,3.$n # ãŒæ£ã—ã„イメージ。ãŸã ã—$ã¯çµ‚端を表ã—ã¦ã„ã‚‹ã‘ã©ã€sortã§ã¯$ãŒãªã„。 # .ã‚’çœç•¥ã™ã‚‹ã®ãŒå”¯ä¸€ã®çµ‚端ã¨ãªã‚‹ã®ã§ã€æœ€çµ‚çš„ã«ã¯ # sort -k 1.1,1 -k 2.2,2n -k 3.2,3n # ãŒæš—é»™ãªã—ã«ã¡ã‚ƒã‚“ã¨æ›¸ã„ãŸå ´åˆã®å‹•ä½œã¨ãªã‚‹ã€‚ # 第二以é™ã®ã‚ーã¯é ã«ã‚¹ãƒ—リッタãŒå…¥ã‚‹ã®ã§2.1ã§ã¯ã‚¹ãƒ—リッタ自体ãŒå…¥ã‚‹ã€‚ # -bãªã‚‰ç©ºç™½ã‚’無視ã™ã‚‹ä¸€èˆ¬çš„ãªå‹•ä½œã«ãªã‚‹ã€‚ # 別ã«ãã‚Œã§ã‚‚実質的ã«ã¯å•é¡Œãªã„ã‘ã©ã€2-4æ–‡å—ã®éƒ¨åˆ†ã ã‘比較ソートã—ãŸã„ãªã‚‰ # 2.2,2.4 ã§ã¯ãªã2.3,2.5ã¨ãªã‚‹ã€‚ # ã¤ã¾ã‚Šã€sortã‚’ãã®ã¾ã¾ç›´æŽ¥ç”¨ã„ã‚‹ã®ã¯è¶…ã‚ã‚“ã©ãã•ã„。 # 自分ã§ä½¿ã„ã‚„ã™ã„エイリアスを作ã£ã¦ãŠãã“ã¨ã‚’念é ã«ä½œã‚‰ã‚Œã¦ã„ã‚‹æ„Ÿã˜ï¼Ÿ #cmd="2n,3,1" #sort -k 1.1,1 -k 2.2,2n -k 3.2,3n func_ssrt() { ( ssrt_cmd="$1" shift ssrt_str=`echo "$ssrt_cmd" | tr -d ' ' | tr ',' '\n' | sed -e 's#^[0123456789]#& #g' | while read -r ssrt_A ssrt_B do if [ $ssrt_A -eq 1 ] ; then echo "-k 1.1,1$ssrt_B" else echo "-k $ssrt_A.1,$ssrt_A$ssrt_B" fi done | tr '\n' ' ' ` sort -b "$@" $ssrt_str ) } #--- main # ssort 2n,3r,1 -a -b ... format pos is fixed to '$1' ( bk_lcl=`set` export LC_ALL=C export LANG=C pf1='( eval "$bk_lcl" ; cat - )' func_ssrt "$@" | eval "$pf1")